0

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

4
  • 1
    What about just calling end() on last call instead of calling on a and b? Commented Aug 12, 2014 at 16:57
  • Yes, that would do it. But, each method must have its own end(). That may not make much sense, because this is a simplified example. But, my application needs and end() for each method @Adherence Commented Aug 12, 2014 at 16:59
  • 1
    This sounds like it would be a rather impossible case. There is no way for the end() method to know when it is being called last. Therefore it wouldn't know when to output the final array. Commented Aug 12, 2014 at 17:02
  • You could grab the contents of the file you're executing PHP from and check if the last end corresponds to the class you're calling it from. In all seriousness it may be time to rethink your strategy. Why do you need this? Commented Aug 12, 2014 at 17:06

1 Answer 1

1

How about this?

<?php class main { private $property = []; private $outputs = 0; function a() { $this->property[$this->outputs][] = 'method: '.__METHOD__.' was called '; return $this; } function b() { $this->property[$this->outputs][] = 'method: '.__METHOD__.' was called '; return $this; } function c() { $this->property[$this->outputs][] = 'method: '.__METHOD__.' was called '; return $this; } function end() { var_dump($this->property); $this->outputs++; } } 

It won't give you the last array, but you can get it from the output if you change the end method:

 function end() { var_dump($this->property[$this->outputs]); $this->outputs++; } 

If you want only 1 array with last call I'm with @Adherence, it's quite impossible without analising your code which complicates (a lot!) this thing...

Sign up to request clarification or add additional context in comments.

4 Comments

This gives three different arrays. Not three values inside one array.
Ok, sorry for too quick answer, how about now?
Actually, now that I think of it. You first answer improved my application.
Sometimes we just miss simple things :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.