PHP magic examples

Source: Internet
Author: User
Tags autoload

PHP magic examples

① _ Get/_ set: takes over the attributes of an object.

When accessing a non-existent object property:

Index. php
Copy codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the root directory constant
Include BASEDIR. '/Common/Loader. php ';
Spl_autoload_register ('\ Common \ Loader: autoload ');

$ Obj = new \ Common \ Object ();

// When accessing a nonexistent object attribute in php
Echo $ obj-> title;

An error is thrown: Notice: Undefined property: Common \ Object ::$ title in D: \ practice \ php \ design \ psr0 \ index. php on line 9.

After the _ set and _ get methods are added to Common/Object. php

Object. php
Copy codeThe Code is as follows:
<? Php
Namespace Common;

Class Object {
Function _ set ($ key, $ value ){
}

Function _ get ($ key ){
}
}

Run index. php again, and no error will be reported.

Modify Common/Object. php again
Copy codeThe Code is 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 codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the root directory constant
Include BASEDIR. '/Common/Loader. php ';
Spl_autoload_register ('\ Common \ Loader: autoload ');

$ Obj = new \ Common \ Object ();

$ Obj-> title = 'hello ';
Echo $ obj-> title;

Run index. php and the page output is as follows:
Copy codeThe Code is as follows:
String 'common \ Object :__ set '(length = 20)
String 'common \ Object :__ get '(length = 20)
Hello

② _ Call/_ callStatic: controls the call of PHP Object methods (_ callStatic is used to control class static methods)

When executing a non-existent php Method

Index. php:
Copy codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the 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 );

Execute index. php reports a Fatal error: Call to undefined method Common \ Object: test () in D: \ practice \ php \ design \ psr0 \ index. php on line 9

If a _ call method is defined in Common/Object, automatic callback is triggered if the method does not exist:
Copy codeThe Code is as follows:
<? Php
Namespace Common;

Class Object {
Function _ call ($ func, $ param) {// $ func method name $ param Parameter
Var_dump ($ func, $ param );
Return "magic function \ n"; // return a string as the return value
}
}

Index. php

Copy codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the 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:
Copy codeThe Code is as follows:
String 'test' (length = 4)
Array
0 => string 'hello' (length = 5)
1 => int 123
Magic function

When a non-existent static method is called

Common/Object. php

Copy codeThe Code is as follows:
<? Php
Namespace Common;

Class Object {
Static function _ callStatic ($ name, $ arguments ){
Var_dump ($ name, $ arguments );
Return "magic function \ n"; // return a string as the return value
}
}

Note: __the callStatic method must also be declared as a static method.

Index. php
Copy codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the root directory constant
Include BASEDIR. '/Common/Loader. php ';
Spl_autoload_register ('\ Common \ Loader: autoload ');

// Execute a non-existent static method
Echo Common \ Object: test ("hello", 1234 );

Run index. php and the page output is as follows:
Copy codeThe 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
Copy codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the root directory constant
Include BASEDIR. '/Common/Loader. php ';
Spl_autoload_register ('\ Common \ Loader: autoload ');

$ Obj = new \ Common \ Object ();

Echo $ obj;

The following error occurs: Catchable fatal error: Object of class Common \ Object cocould not be converted to string in D: \ practice \ php \ design \ psr0 \ index. php on line 8.

Add the _ toString method to Object. php.
Copy codeThe Code is as follows:
<? Php
Namespace Common;

Class Object {
Function _ toString (){
Return _ CLASS __;
}
}

④ _ Invoke: This magic method is called back when a PHP Object is executed as a function.

Index. php
Copy codeThe Code is as follows:
<? Php
Define ('basedir' ,__ DIR _); // defines the root directory constant
Include BASEDIR. '/Common/Loader. php ';
Spl_autoload_register ('\ Common \ Loader: autoload ');

$ Obj = new \ Common \ Object ();

Echo $ obj ("test ");

Object. php
Copy codeThe Code is as follows:
<? Php
Namespace Common;

Class Object {
Function _ invoke ($ param ){
Var_dump ($ param );
Return 'invoke ';
}
}

Page output:
Copy codeThe 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.