PHP inside with magic method and anonymous function closure function dynamically add a method to the class

Source: Internet
Author: User
Tags closure

1. Recognize __set (__set () is called when assigning a value to an unreachable property)

This means that you revisit a property that is not in a class.

 class   a{  private      $aa  = ' one '  public  function  __set ( $name ,  $value    $this -> $name  =  $value  ; }}   $a  = new   A ();   $a ->name = ' name ' ;  echo   $a ->name; 

2. Recognize __set (__call () is called when an inaccessible method is called in the object. )

class b{    private$bb = ' a ';
Public function __call ($name$arguments) { echo$name ; Var_dump ($arguments);} } $b New B (); $b->names ();

3. Dynamic Add method

classb{Private $BB= ' 22 ';  Public function__set ($name,$value)    {        $this-$name=$value; }     Public function__call ($name,$arguments)    {//Note: No formal parameter $name        return Call_user_func($this-$name,$arguments);//by adding an anonymous method to the attribute note: $arguments is an array    }}$b=NewB ();$b->names =function(){Echo' This was a fun ';};$b-names ();//At this time , there is already a property in class B that refers to an anonymous method//How to run it? $b->name () error because there is no such method in the class---this error can trigger the __call () Magic method//This time not in the anonymous function with the properties of Class B 

4. Dynamic addition method, so that the closure function can also manipulate the property reference inside the class (http://php.net/manual/zh/closure.bindto.php)

classc{Private $CC= ' 33 ';  Public function__set ($name,$value)    {        //$this $name = $value;//(This is changed by comparing the example above)        $this-$name=$value->bindto ($this,$this);//copies the current closure function, binding the specified $this scope object so that the anonymous function can access the property values of the class    }     Public function__call ($name,$arguments)    {        return Call_user_func($this-$name,$arguments); }}$c=NewC ();$c->username =function($strs){    Var_dump($strs);//This is actually Call_user_func's $arguments , the array.    $this->cc=4;//property values that can be manipulated for    return' 111 ';};Echo $c->username (' string ');

A complete example:

/** * Add New method to class dynamically * * @author fantasy*/trait dynamictrait {/** * methods that exist in the auto-call class*/     Public function__call ($name,$args) {        if(is_callable($this-$name)){            return Call_user_func($this-$name,$args); }Else{            Throw New\runtimeexception ("Method {$name} does not exist "); }    }    /** * Add method*/     Public function__set ($name,$value) {        $this-$name=is_callable($value)?$value->bindto ($this,$this):$value; }}/** * only with attributes without method animal class * * @author fantasy*/classAnimal { Usedynamictrait; Private $dog= ' Barking Team ';}$animal=NewAnimal;//add a method to the animal instance to get the private property of the instance $dog$animal->getdog =function() {    return $this-Dog;};Echo $animal->getdog ();//Output Barking Team

Dynamic to the class inside Add method, is to put a closure function through __set and __call combined with call_user_func () and other methods, add into,

In order for an anonymous function or a closure function to access the property values of a class, you need to combine the closure::bindto of the closure class (copy the current package object, bind to the specified this scope)

Reference: http://www.cnblogs.com/fps2tao/p/8727248.html

PHP inside with magic method and anonymous function closure function dynamically add a method to the class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.