Basically what I'm trying to achieve is to make the object reference another object of the same class - from inside of a method. It'd be perfect if the following would work:
$this = new self;
But one cannot reassign $this
in php.
I of course know, I can return another object from the method and use that, so please do not advise that. The question is:
How can I make $this
to be a clone of another object of the same class?
or more specifically,
I want an object to revert to a specific state that was previously saved.
EDIT: some examples where this might be useful.
Say you have an Url object that accepts controller, action and lots more things. You are going to have it fetch a lot of links with the same, say, controller and action, but other properties will differ. I instantiate the object with the common parameters, call a method for it to save its state which it reverts to after outputting a link (IOW after __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 is I have a CRUD table and I have to configure each column as an object I pass to the main table object. After passing the column I launch a reset method on the column, so I can have code like the following:
$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, don't you think?