PHP Magic Method Use Example _php instance

Source: Internet
Author: User

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

When you access an object property that does not exist:

index.php

Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
Include BASEDIR. ' /common/loader.php ';
Spl_autoload_register (' \\common\\loader::autoload ');

$obj = new \common\object ();

When you access a nonexistent 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

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

object.php

Copy Code code as follows:

<?php
namespace Common;

Class object{
function __set ($key, $value) {
}

function __get ($key) {
}
}

Again executes index.php, no more error.

Modify common/object.php again

Copy Code code as follows:

<?php
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
Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
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:

Copy Code code as follows:

String ' Common\object::__set ' (length=20)
String ' Common\object::__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:

Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
Include BASEDIR. ' /common/loader.php ';
Spl_autoload_register (' \\common\\loader::autoload ');

$obj = new \common\object ();

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 common\object::test () in D:\practise\php\design\psr0\ index.php on line 9

If a __call method is defined in Common/object, it is automatically recalled when the method does not exist:

Copy Code code as follows:

<?php
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

Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
Include BASEDIR. ' /common/loader.php ';
Spl_autoload_register (' \\common\\loader::autoload ');

$obj = new \common\object ();

When you execute a PHP method that does not exist
echo $obj->test (' Hello ', 123);

Page output:

Copy Code code 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

Copy Code code as follows:

<?php
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

Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
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:

Copy Code code 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

Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
Include BASEDIR. ' /common/loader.php ';
Spl_autoload_register (' \\common\\loader::autoload ');

$obj = new \common\object ();

Echo $obj;

Error: Catchable fatal error:object of class Common\object could not is converted to string in D:\PRACTISE\PHP\DESIGN\PSR 0\index.php on line 8

Add __tostring method to object.php

Copy Code code as follows:

<?php
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

Copy Code code as follows:

<?php
Define (' BASEDIR ', __dir__); Defining Root Constants
Include BASEDIR. ' /common/loader.php ';
Spl_autoload_register (' \\common\\loader::autoload ');

$obj = new \common\object ();

echo $obj ("test");


object.php
Copy Code code as follows:

<?php
namespace Common;

Class object{
function __invoke ($param) {
Var_dump ($param);
Return ' Invoke ';
}
}

Page output:

Copy Code code 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.