This article describes how to use PHP magic methods, this article describes how to use magic methods such as _ get, _ set, _ call, _ callStatic, _ toString, and _ invoke. For more information, see
① _ Get/_ set: takes over the attributes of an object.
When accessing a non-existent object property:
Index. php
The 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
The 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
The 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
The 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:
The 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:
The 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:
The 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
The 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:
The 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
The 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
The 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:
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:
<? 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.
The 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
The 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
The Code is as follows:
<? Php
Namespace Common;
Class Object {
Function _ invoke ($ param ){
Var_dump ($ param );
Return 'invoke ';
}
}
Page output:
The Code is as follows:
String 'test' (length = 4)
Invoke