This article describes MagicMethod in PHP, including _ construct (), _ destruct (), and _ call (), _ callStatic (), _ get (), _ set (), and _ toS
This article mainly introduces the Magic Method in PHP, including _ construct (), _ destruct (), _ call (), _ callStatic (), _ get (), _ set (), and _ toS
1. _ construct () and _ destruct ()
If an instance is called when it is created or destroyed, zero or multiple parameters can be passed.
Class A {function A () {echo "build A";} function _ destruct () {echo "destroy A" ;}}$ obj = new (); // unset ($ obj );
Note: The destructor method will be called as soon as there are no other references to a participant object, or in any order during the shutdown sequence.
With regards to constructor, a class defined in a particular namespace named after the class name will no longer be considered as a constructor at the beginning of PHP5.3.3. In a namespace-free class, it is still a constructor. For example:
Namespace Foo; class Bar {public function Bar () {// treated as constructor in PHP 5.3.0-5.3.2 // treated as regular method as of PHP 5.3.3 }}
If namespace Foo; is not available, Bar () is also treated as a constructor. In addition, if the following conditions exist:
Function _ construct () {echo "construct A";} function A () {echo "build A";} function _ destruct () {echo "destroy ";}}
That is, if _ construct () and a function with the same name as the class name are included, only _ construct () is called ().
2. _ call () and _ callStatic ()
This method is called when you try to call a method that does not exist. Two parameters, one is the method name and the other is the parameter array of the called method.
Class MethodTest {public function _ call ($ name, $ arguments) {// Note: value of $ name is case sensitive. echo "Calling object method '$ name '". implode ('', $ arguments )."
";} Public static function _ callStatic ($ name, $ arguments) {// Note: value of $ name is case sensitive. echo "Calling static method '$ name '". implode ('', $ arguments )."
";}}$ Obj = new MethodTest; $ obj-> runTest ('in', 'object', 'context'); MethodTest: runTest ('in ', 'static ', 'context ');
$ Arguments is passed as an array. Running result:
Calling object method 'runtest' in object context
Calling static method 'runtest' in static context
Also note the function scopes protected and private:
Class TestMagicCallMethod {public function foo () {echo _ method=.php_eol ."
";} Public function _ call ($ method, $ args) {echo _ method).php_eol ."
"; If (method_exists ($ this, $ method) {$ this-> $ method () ;}} protected function bar () {echo _ method=.php_eol ."
";} Private function baz () {echo _ method#.php_eol ."
";}}$ Test = new TestMagicCallMethod (); $ test-> foo ();/*** Outputs: * TestMagicCallMethod :: foo */$ test-> bar ();/*** Outputs: * TestMagicCallMethod :__ call * TestMagicCallMethod: bar */$ test-> baz (); /*** Outputs: * TestMagicCallMethod :__ call * TestMagicCallMethod: baz */
3. _ get () and _ set ()
It is called when you try to read an attribute that does not exist in an object.
Note: We can use this function to perform operations similar to reflection in java.
Class Test {public function _ get ($ key) {echo $ key. "not exists";} public function _ set ($ key, $ value) {echo $ key. "= ". $ value ;}}$ t = new Test (); echo $ t-> name."
"; $ T-> name =" abc ";
Output:
Name not exists
Name = abc
4. _ toString ()
This method is similar to the toString () method of java. When we print the object directly, we call this function back. The function must return a string.
Class Test {private $ name = "abc"; private $ age = 12; public function _ toString () {return "name: $ this-> name, age: $ this-> age ";}}$ t = new Test (); echo $ t;
Output:
Name: abc, age: 12