Skip to content
Advertisement

PHP: The name of the previous method in chain

I am method chaining in PHP, something like this:

JavaScript

Is there any way the colourMethod() can know if it was called after the blackMethod() or whiteMethod()? In other words, is there a way of getting the name of the previously called method in a chain of methods?

Something like this:

JavaScript

I understand that chaining is only shorthand for calling multiple methods from the same class, but I was hoping there is a way to link the chains together somehow.

I have tried debug_backtrace() and

JavaScript

but they only give class names or the name of the method that called the colourMethod (which is $c), not the method that was called before it.

Advertisement

Answer

A good and more general way to get a track of the previously called method is to use a private or protected property to keep track of the actual method using the constante __FUNCTION__ or __METHOD__
example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement