I have this difficult problem, which may be solved using references. I have tried to put this &
reference symbol, just about everywhere, albeit to no avail.
Here is a simplified script to demonstrate my base application.
<?php
class main
{
private $property = [];
function a()
{
$this->property[] = 'method: '.__METHOD__.' was called ';
return $this;
}
function b()
{
$this->property[] = 'method: '.__METHOD__.' was called ';
return $this;
}
function c()
{
$this->property[] = 'method: '.__METHOD__.' was called ';
return $this;
}
function end()
{
var_dump($this->property);
}
}
As you can see, it is a simple class with its methods, all adding value to one class property, all the methods return the class object (are chainable), except for the end()
method.
Now, for the purpose of my application, I have to make calls to the class methods
$a = new main;
$a->a()->end();
$a->b()->end();
$a->c()->end();
Now, the problem as you can see, the output will be something like this.
array(1) {
[0]=>
string(27) "method: main::a was called "
}
array(2) {
[0]=>
string(27) "method: main::a was called "
[1]=>
string(27) "method: main::b was called "
}
array(3) {
[0]=>
string(27) "method: main::a was called "
[1]=>
string(27) "method: main::b was called "
[2]=>
string(27) "method: main::c was called "
}
What I am looking for is, to only get the last array. that is:
array(3) {
[0]=>
string(27) "method: main::a was called "
[1]=>
string(27) "method: main::b was called "
[2]=>
string(27) "method: main::c was called "
}
Because, as shown in my previous code, I am calling the function this way.
$a = new main;
$a->a()->end();
$a->b()->end();
$a->c()->end();
And it makes sense to get the last array, instead of the other two. I realize, one way to achieve this would be, to start the object three time, as in
(new main)->a()->end();
(new main)->b()->end();
(new main)->c()->end();
But, I am hoping, that somewhere in-between, using clone
or reference
, it might be possible to get only the last array.
Thanks