PHP design Pattern notes and summaries (5) Use of the PHP magic method

Source: Internet
Author: User
Tags autoload

Use of the PHP magic method

①__get/__set: taking over the properties of an object

When accessing a non-existent object property:

index.php

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $obj new \common\Object();   // when accessing a non-existent object property in PHP Echo $obj->title;

Throws an error: notice:undefined property:common\object:: $title in D:\practise\php\design\psr0\index.php on line 9

After you add the __set and __get methods in common/object.php

object.php

<? phpnamespace Common; class Object {    function __set ($key,$value) {    }        function __get ($key) {    }}

Execute index.php again, no more error.

Modify common/object.php again

<?phpnamespace Common;class Object{    protected $array=Array(); function__set ($key,$value){        Var_dump(__method__); $this-Array[$key] =$value; }        function__get ($key){        Var_dump(__method__); return $this-Array[$key]; }}

index.php

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $obj new \common\Object();   $obj->title = ' Hello '; Echo $obj->title;

Execute index.php, page output:

string ' Common\object::__set ' (length=20)string ' Common\object::__get ' (length=20) Hello

②__call/__callstatic: A call to control the PHP object method (__callstatic is used to control the static method of the Class)

When executing a non-existent PHP method

index.php:

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $obj new \common\Object();   // When executing a non-existent PHP method $obj->test (' Hello ', 123);

Execution index.php will report a fatal error: Fatal error:call to undefined method common\object::test () in D:\practise\php\design\psr0\ index.php on line 9

If you define a __call method in Common/object, the callback is automatically when the method does not exist:

<? phpnamespace Common; class Object {        function __call ($func$param//$func method name $param parameter        Var_dump ($func$param);         return // returns a string as the return value     }}

index.php

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $obj new \common\Object();   // When executing a non-existent PHP method Echo $obj->test (' Hello ', 123);

Page output:

string ' Test ' (length=4)array  string ' Hello ' (length=5)  1 = int 123   function

When a static method that does not exist is called

common/object.php

<? phpnamespace Common; class Object {    staticfunction __callstatic ($name$arguments) {         Var_dump ($name$arguments);         return // returns a string as the return        value     }}

Note: The __callstatic method is also declared as a static method

index.php

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); // executes a static method that does not exist Echo common\Object:: Test ("Hello", 1234);

Execute index.php, page output:

string ' Test ' (length=4)array  string ' Hello ' (length=5)  1 = int 1234   function

③__tostring: Converts a PHP object into a string

index.php

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $obj new \common\Object(); Echo $obj;

This will cause an error: Catchable fatal error:object of class Common\object could not being converted to string in D:\PRACTISE\PHP\DESIGN\PSR 0\index.php on line 8

Adding __tostring methods in object.php

<? phpnamespace Common; class Object {    function  __tostring () {        return__class__;    }}

At this point, execute index.php, output:

common\Object

④__invoke: When a PHP object is executed as a function, this magic method is recalled

index.php

<? PHP Define // defining a root directory constant include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $obj new \common\Object(); Echo $obj ("Test");

object.php

<? phpnamespace Common; class Object {    function __invoke ($param) {        var_dump($param );         return ' Invoke ';    }}

Page output:

string ' Test ' (length=4) Invoke

PHP design Pattern notes and summaries (5) Use of the PHP magic method

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.