Examples of using PHP magic methods

Source: Internet
Author: User
Tags autoload

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

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

When you access an object property that does not exist:

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); Defining Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

$obj = new Commonobject ();

When you access a nonexistent object property in PHP

Echo $obj->title;

Throws an error: notice:undefined property:commonobject:: $title in d:practisephpdesignpsr0index.php on line 9

When the __set and __get methods are added to the common/object.php

object.php

The code is as follows:

  

namespace Common;

Class object{

function __set ($key, $value) {

}

function __get ($key) {

}

}

Again executes index.php, 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 Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

$obj = new Commonobject ();

$obj->title = ' Hello ';

Echo $obj->title;

Execute index.php, page output:

The code is as follows:

String ' Commonobject::__set ' (length=20)

String ' Commonobject::__get ' (length=20)

Hello

②__call/__callstatic: Controls the invocation of the PHP object method (__callstatic to control the static method of the Class)

When you execute a PHP method that does not exist

index.php:

The code is as follows:

  

Define (' BASEDIR ', __dir__); Defining Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

$obj = new Commonobject ();

When you execute a PHP method that does not exist

$obj->test (' Hello ', 123);

Execution index.php will report a fatal error: Fatal error:call to undefined method commonobject::test () into d:practisephpdesignpsr0index.php on Li NE 9

If a __call method is defined in Common/object, it is automatically recalled 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 Functionn"; Returns a string as the return value

}

}

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); Defining Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

$obj = new Commonobject ();

When you execute a PHP method that does not exist

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 you call a static method that does not exist

common/object.php

The code is as follows:

  

namespace Common;

Class object{

static function __callstatic ($name, $arguments) {

Var_dump ($name, $arguments);

Return "Magic Functionn"; 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 Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

Executes a static method that does not exist

echo commonobject::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 Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

$obj = new Commonobject ();

Echo $obj;

Error: Catchable fatal error:object of class Commonobject could not is converted to string in D:practisephpdesignpsr0inde X.php on line 8

Add __tostring method to 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, the Magic method is recalled

index.php

The code is as follows:

  

Define (' BASEDIR ', __dir__); Defining Root Constants

Include BASEDIR. ' /common/loader.php ';

Spl_autoload_register (' commonloader::autoload ');

$obj = new Commonobject ();

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

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.