PHP overloaded [magic method] - opening

Excerpts from the official website of PHP magic methods of interpretation:
PHP provided by the "heavy-duty" (overloading) means to dynamically "create" class properties and methods. We are the method by magic (magic methods) to achieve.
When a class property or method call is not defined under the current environment or invisible, overloaded method will be called. Later in this section using the "inaccessible property (inaccessible properties)" and "non-access method (inaccessible methods)" to call these class property or method undefined or not visible.
All overloading methods must be declared as public.

Note:
These parameters are magic methods are not passed by reference.

Note:
PHP is "overloaded" with most other object-oriented languages. Traditional "overloading" class method for providing a plurality of the same name, but each process is different, and the number of parameter types.

Properties overloaded
public __set (String $ name, $ Mixed value): void
public __get (String $ name): Mixed
public __isset (String $ name): BOOL
public __unset (String $ name):

void when to inaccessible property assignment, __ set () is called.
When reading the value of the property is not accessible, __ get () is called.
When calling isset to inaccessible property () or empty (), __ isset () is called.
When you call unset on inaccessible properties (), __ unset () is called.
$ Name parameter is the name of the variable simply more action. $ value parameter __set () method specifies the value of $ name variable.
Properties can only be overloaded in the subject. In the static method, these magic methods will not be called. Therefore, these methods can be declared as static.

Note:
Because PHP handles the assignment operator, __ set () return value is ignored. Similarly, such a chain assignments below, __ GET () is not called:
$ A $ obj- => B =. 8;

Note:
Unable to use other languages overloaded attribute structures other isset () in the outer , this means that when an overloaded property to use empty (), reload magic methods will not be called.
In order to avoid this limitation, overloaded property must be assigned to a local variable and then use the empty ().

Example # 1 using __get (), __ set () , __ isset () and __unset () attribute reload

class PropertyTest
{
    / * * Overloaded data stored here   * / 
    Private  $ Data = Array ();

    / * * Can not be used in heavy-duty defined attribute   * / 
    public  $ declared =. 1 ;

    / * * Only when this property is accessed from outside the class, overloading will occur * / 
    Private  $ hidden = 2 ;

    public function __set ($name, $value)
    {
        $this->data[$name] = $value;
    }

    public function __get ($name)
    {
        if (isset($this->$name)) {
            return $this->$name;
        }
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
        // generates a traceback 
        $ the trace = debug_backtrace ();
         // throws an exception 
        the trigger_error ( 'Property Via the __get Undefined ():'. $ Name 'in'.. $ The trace [0] [ 'File'] 'ON. Line '. $ the trace [0] [' Line '], E_USER_NOTICE );
         return  null ;
    }

    public function __isset ($name)
    {
        return isset($this->data[$name]);
    }

    public function __unset ($name)
    {
        unset($this->data[$name]);
    }

    / * * Non-magic method   * / 
    public  function getHidden ()
    {
        return $this->hidden;
    }
}

$obj = new PropertyTest;

// output a variable that does not exist, go __get (), the exception will be thrown 
echo  $ obj -> a;

// for a non-existent variable value of 1, will come __set () of 
$ obj -> a = 1 ;

// output variable a again, since the __set thereto the above (), so this is a value to be accessed. 1 
echo  $ obj -> a;

// At this time, when using isset () of a variable does not exist calculates, will come The __isset (), since the __set thereto the above (), it is to true 
var_dump ( isset ( $ obj -> a) );

// When a conduct unset (), will come __unset () in the 
unset ( $ obj -> a);

// then subjected isset (), this case does not exist 
var_dump ( isset ( $ obj -> A));

// variable access private property, will enter __get () in 
echo  $ obj -> hidden;


Method overloading
public __call (String $ name, $ arguments The Array): Mixed
public static __callStatic (String $ name, $ arguments The Array): Mixed

When you call a method not accessible in the object, __ call () will be called.
When you call a method not accessible in a static context, __ callStatic () is called.
$ name parameter is the name of the method to invoke.
$ arguments argument is an enumerated array containing the parameters to be passed to the method of $ name.

Example # 2 using the __call () and __callStatic () method overloading

class MethodTest
{
    /**
     * Enter here when you call a method that does not exist
     * @param $name
     * @param $arguments
     */
    public function __call ($name, $arguments)
    {
        // Note: value of $ name is case-sensitive 
        $ info = [
             'name' => $ name ,
            'arguments' => $arguments,
        ];
        print_r($info);
    }

    /**
     * PHP version 5.3.0 after
     * When you call the static method does not exist, enter here
     */
    public static function __callStatic ($name, $arguments)
    {
        // Note: value of $ name is case-sensitive 
        $ info = [
             'name' => $ name ,
            'arguments' => $arguments,
        ];
        print_r($info);
    }
}

$arguments = ['A', 'B', 'C'];

$obj = new MethodTest;
$obj->test(...$arguments);

MethodTest :: the Test (... $ arguments The );   // After PHP 5.3.0 version

/*
 * Two or more outputs are:
 *  Array
    (
        [name] => test
        [arguments] => Array
            (
                [0] => A
                [1] => B
                [2] => C
            )

    )
 */



Guess you like

Origin www.cnblogs.com/deverz/p/11093218.html