php - Closure How to use instead of the class method? -


how write code works in following example without __get , __set.everything else can.

class {     // code write here? }  $a = new a();  $a->setname = function(a $a, $value) {     $a->name = $value; };  $a->getname = function(a $a) {     return $a->name; };  $a->setname('vasya'); echo $a->getname1(); 

friends, solved problem

class {     public function __call($name, $arguments) {         try{             if(property_exists($this,$name) && is_callable($this->$name)){                 $arguments = array_merge([__class__=>$this],$arguments);                 return call_user_func_array($this->$name, $arguments);             }             else{                 throw new exception($name.' not callable');             }         }         catch (exception $e)         {             echo $e->getmessage();             exit;         }     } }  $a = new a();  $a->setname = function(a $a, $value) {     $a->name = $value; };  $a->getname = function(a $a) {     return $a->name; };  $a->setname('john'); echo $a->getname(); 

http://php.net/manual/ru/functions.anonymous.php#117504


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -