Is there a way to get the order in which the OOP method is called?

For example, let's say I have a set of classes and methods for this:

$obj->method1()->method2();

In any case, for method1 () to know by itself that its first method is called, or for method2 to know that its last?

Additional information
I just want to be able to create a set of these calls so that it either returns an instance of itself if the method call is not at the end of the chain, or returns something else if it is at the end.

for example

$obj->method1()->method2(); #Here method 2 will return lets say a string.
$obj->method1()->method2()->method3(); #Since method2 isnt at the end of the chain, it should return an instance of itself (or another object) here so that the chain could continue.

EDIT: any whoz trying to do this is a bad design.

It looks like a duplicate. See this question for more details .

+3
source share
4 answers

, , , , - . , , :

$obj->m1()->m2();
$obj->m3(); // You would think that m1() and m2() came before this in the same chain

, .

, , , .

, , PHP ( , ). . , , - .

, - . - , - :

$meta = new ImageMeta();
$meta->first_name("foo")->last_name("bar")->email("baz")->id("guid");

:

$meta->first_name();

. NULL, NULL, . ( ) $this.

, , , . . / one, .
0

, .

, - , :

!

$GLOBALS["chain"] = array();
$obj->method1()->method2();    // method1 adds member to $GLOBALS["chain"], 
                               // array_push($GLOBALS["chain"], __FUNCTION__);
                               // method2 does the same...
print_r($GLOBALS["chain"]);

- , , method2().

.

, ?

+1

, , - , .

( : ? , ?)

0

- .

, .

, . .

0

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


All Articles