0

I would like to know, while using method chaining in PHP, if a last chained method, can possibly return its parent.

Here is an example.

class foo
{
    function a(){
        return $this; 
    }

    function b()
    {
        return $this;
    }

    function c()
    {
        return $this; 
    }
}

echo (new foo)->c()->a(); // c

In the above example, you can see, that a() is to output 'c'. I can/have done this using arguments, or static properties, but I am thinking if it can be done another way, specially using Reflections which I happen to know so little about.

robue-a7119895
  • 816
  • 2
  • 11
  • 31
  • Natively - no. And if you need that (as well as "chaining" as a core solution) - then it's time to reconsider your logic & architecture. It's just.. bad. It's unreadable, non-scalable and unreliable – Alma Do Aug 11 '14 at 18:36
  • I don't think it's that bad. There are quite functions that simulate the above logic in a class context, as there should for methods. – robue-a7119895 Aug 11 '14 at 18:38
  • What do you mean by `parent`? The return of the previously called function? Maybe explain a little better. – AbraCadaver Aug 11 '14 at 18:44
  • @AbraCadaver Yes. The previous function name. – robue-a7119895 Aug 11 '14 at 18:46

1 Answers1

0

Unfortunately I have got some -1 for my helps, so I cannot comment, but I can write answer though! :)

Check this post:

how to get function name inside a function in PHP?

and if you get your answer, yeah... help me :)

Community
  • 1
  • 1
Soley
  • 1,716
  • 1
  • 19
  • 33
  • You should really read the question. It isn't related to getting function name, it;s about chaining & tracing that – Alma Do Aug 11 '14 at 18:39
  • WHY NOT USING http://stackoverflow.com/a/1006290/2655623 ? it contains the answer. – Soley Aug 11 '14 at 18:42
  • First - because it's awful suggestion in that answer. Second - because won't work in all cases. And third - because - why copy existing content?.. – Alma Do Aug 11 '14 at 18:47