PHP sets the value of $ this

Basically, I'm trying to make an object a reference another object of the same class - from within the method. It would be ideal if the following worked:

$this = new self;

But you cannot reassign $thisin php.

Of course, I know, I can return another object from the method and use it, so please do not advise this. The question arises:

How can I do $thisto be a clone of another object of the same class?

or more specifically

I want the object to return to a specific state that was previously saved.


EDIT : some examples where this might be useful.

Let's say you have an Url object that takes a controller, an action, and more. You are going to get a lot of links with the same, say, controller and action, but other properties will be different. I create an instance of the object using the general parameters, call the method to save its state, which it returns after the link is displayed (IOW after the __toString method).

$defaultPath = Url::factory('controller','action1')->extraParams('status',0)->save();

echo $defaultPath->action('action2'); // this has action2 as action and a status 0 as extra params
echo $defaultPath->extraParams('status',2); // this has action1 as action and 2 as status

Another use: I have a CRUD table, and I have to configure each column as an object that I pass to the main table object. After passing the column, I run the reset method on the column, so I can have the following code:

    $column->field = 'layouts_id';
    $column->value = $layoutId;
    $dbInput->addColumn($column);

    $column->field = 'pages_id';
    $column->value = $pagesId;
    $dbInput->addColumn($column);

In both situations, I save a lot of code and confusion, right?

+3
source share
5 answers

, :

$this, , , , , ? ,

$column->field = 'layouts_id';
$column->value = $layoutId;
$dbInput->addColumn($column);

$column->field = 'pages_id';
$column->value = $pagesId;
$dbInput->addColumn($column);

, $column ?

$column->field = 'layouts_id';
$column->value = $layoutId;
$dbInput->addColumn($column);
// log my value
$logger->log($column); // oops, it empty

...

, imho .


, :
// clone when passing on
$column->field = 'value_id';
$column->value = $value;
$dbInput->addColumn(clone $column);

// manually (re-)create a new object based on an older
$column->field = 'value_id';
$column->value = $value;
$dbInput->addColumn(new Column($column));

</" > reset() :

class Column() {

    public function reset() {
        $this->field = 'standard field id';
        $this->value = 'standard field value';
    }

    public function __construct() {
        $this->reset();
    }

}

:

// manually (re-)create a new object based on an older
$column->field = 'value_id';
$column->value = $value;
$dbInput->addColumn($column);
$column->reset();

</" > ():

class Column() {

    public $field = 'standard field id';
    public $value = 'standard field value';

    // keep a static object for resetting
    private static $__blueprint = null;

    public function reset() {
        foreach (self :: $__blueprint as $k => $v)
            $this->$k = $v;
    }

    public function __construct() {
        if (!isset(self :: $__blueprint))
            self :: $__blueprint = clone $this;
    }

}
+3

$this = new self; , reset .

, , , reset(), , __constructor().

, ( ), . , , . , , , . , , , . , , . , ... , ...;)

+2

, , . this ( OOP) . .

+1

$this PHP. .

, , . .

, , - $copy . , $copy . .

+1

, , . $this, , ...

A couple that is with a pool of objects design template and you have to be golden ...

+1
source

Source: https://habr.com/ru/post/1771763/


All Articles