Examples of using PHP Magic methods _php Tutorial

Source: Internet
Author: User
Tags autoload

Examples of using the PHP magic method


This article mainly introduces the use of PHP magic methods, this article explains the use of __get, __set, __call, __callstatic, __tostring, __invoke and other magic methods, the need for friends can refer to the following

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

When accessing a non-existent object property:

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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

The code is as follows:

  

namespace Common;

Class object{

function __set ($key, $value) {

}

function __get ($key) {

}

}

Execute index.php again, no more error.

Modify common/object.php again

The code is as follows:

  

namespace 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

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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:

The code is as follows:

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:

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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:

The code is as follows:

  

namespace Common;

Class object{

function __call ($func, $param) {//$func method name $param parameter

Var_dump ($func, $param);

Return "Magic function\n"; Returns a string as the return value

}

}

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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:

The code is as follows:

String ' Test ' (length=4)

Array

0 = String ' Hello ' (length=5)

1 = int 123

Magic function

When a static method that does not exist is called

common/object.php

The code is as follows:

  

namespace Common;

Class object{

static function __callstatic ($name, $arguments) {

Var_dump ($name, $arguments);

Return "Magic function\n"; Returns a string as the return value

}

}

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

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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:

The code is as follows:

String ' Test ' (length=4)

Array

0 = String ' Hello ' (length=5)

1 = int 1234

Magic function

③__tostring: Converts a PHP object into a string

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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

The code is as follows:

  

namespace Common;

Class object{

function __tostring () {

return __class__;

}

}

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

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); 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

The code is as follows:

  

namespace Common;

Class object{

function __invoke ($param) {

Var_dump ($param);

Return ' Invoke ';

}

}

Page output:

The code is as follows:

String ' Test ' (length=4)

Invoke

http://www.bkjia.com/PHPjc/1020268.html www.bkjia.com true http://www.bkjia.com/PHPjc/1020268.html techarticle examples of use of the PHP magic method This article mainly introduces the use of the PHP Magic Method Example, this article explains the __get, __set, __call, __callstatic, __tostring, __invoke and other magic square ...

  • Related Article

    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.