Php magic methods and php magic

Source: Internet
Author: User

Php magic methods and php magic

In PHP versions later than PHP 5, classes in PHP can use magic methods. It specifies that all methods starting with two underscores (_) are retained as magic methods. Therefore, we recommend that you do not start with _ In the function name unless you want to reload the existing magic methods. PHP retains all class Methods Starting with _ (two underscores) as magic methods.

_ ToString () and _ invoke ()

Public string _ toString (void): This method is automatically called when an object is used as a string. This method must return a string

Copy codeThe Code is as follows:
<? Php
Class Magic {
Public function _ tostring (){
Return "hello world! ";
}
}
$ Obj = new Magic ();
Echo $ obj; // hello world!
?>

_ Invoke (): This method is automatically called when an object is called as a method.

Copy codeThe Code is as follows:
<? Php
Class Magic {
Public function _ tostring (){
Return "hello world! ";
}
Public function _ invoke ($ x ){
Echo "_ invoke called with param". $ x. "\ n ";
}
}
$ Obj = new Magic ();
$ Obj (10); // _ invoke called with param 10
?>

_ Call () and _ callStatic ()

_ Call (): when an object accesses a method name that does not exist, the __call () method is automatically called.

_ CallStatic (): When the object accesses a static method name that does not exist, the __callstatic () method is automatically called.

Through these two methods, the call of the same method name can correspond to different method implementations

Copy codeThe Code is as follows:
<? Php
Class Magic {
// The '$ name' parameter is the name of the method to be called. The '$ arguments' parameter is an enumerated array,
// Contains the parameter to be passed to the method '$ name.
Public function _ call ($ name, $ arguments ){
// The implode () function combines array elements into a string. Implode (separator, array)
Echo "Calling". $ name. "with param:". implode (",", $ arguments). "\ n ";
}
}
$ Obj = new Magic ();
$ Obj-> run ("para1", "para2"); // obj calls the run method. Output: Calling run with param: para1, para2
?>

_ Get () and _ set ()

When assigning values to inaccessible properties, __set () will be called
When you read the value of an inaccessible attribute, __get () will be called

Copy codeThe Code is as follows:
<? Php
Class Magic {
// There must be a static keyword before the function
Public function _ get ($ name ){
Return "Getting the property". $ name;
}
}
$ Obj = new Magic ();
Echo $ obj-> className. "\ n"; // Getting the property className
?>

When you read the value of an inaccessible attribute, __get () will be called

Copy codeThe Code is as follows:
<? Php
Class Magic {

Public function _ set ($ name, $ value ){
Echo "Setting the property". $ name. "to value". $ value. "\ n ";
}
}
$ Obj = new Magic ();
$ Obj-> className = 'magicclass'; // Setting the property classNameto value MagicClass
?>

_ Isset () and _ unset ()

When isset () or empty () is called for an inaccessible attribute, __isset () is called.
When unset () is called for an inaccessible attribute, __unset () is called.

Copy codeThe Code is as follows:
<? Php
Class Magic {
Public function _ isset ($ name ){
Echo "_ isset invoked \ n ";
Return true;
}
}
$ Obj = new Magic ();
Echo '$ obj-> className is set? '. Isset ($ obj-> className). "\ n"; // _ isset invoked $ obj-> className is set? 1
?>

The above is an introduction and example of eight php object-oriented magic methods, hoping to help you

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.