Answer the question
In order to leave comments, you need to log in
PHP chaining
Hello!
Surely many of you are familiar with the Zend Framework and know about the use of the chain method in its classes. I would like to know if it is possible to somehow magically implement a class in php in such a way that ... So that I inherit from this class, write my methods and my magic class implements the chain, that is, it calls my method, processes and returns $this . It's not too lazy to write an extra line - so far it's just interest. It's important that I don't have to give class methods any prefixes or anything like that.
Answer the question
In order to leave comments, you need to log in
public function __call($method,$args){
if (!in_array($method,$this->publicChainingMethods)) return null;
call_user_func_array ( array($this,$method.'_real') , $args );
return $this;
}
It's simple, for __call to work on existing functions, it is enough to make them private or protected, and they will be available for calling, but! Then there is a security hole - you can call any methods. How to solve the problem?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question