PHP Magic method Detailed, PHP magic detailed _php Tutorial

Source: Internet
Author: User

PHP Magic method Detailed, PHP magic explanation


From PHP 5 onwards, classes in PHP can use the Magic method. The rules that begin with two underscores (__) are preserved as magic methods, so it is recommended that you do not start with the function name unless you are trying to reload the existing magic method. PHP retains all class methods that begin with _ _ (two underscores) as a magic method.

__tostring () and __invoke ()

public string __tostring (void): This method is called automatically when the object is used as a string. This method must return a string

Copy the Code code as follows:
<?php
Class magic{
Public Function __tostring () {
Return "Hello world!";
}
}
$obj = new Magic ();
echo $obj;//hello world!
?>

__invoke (): This method is called automatically when the object is called as a method.

Copy the Code code 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,//__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 an object accesses a static method name that does not exist, the __callstatic () method is automatically called

With these two methods, the invocation of the name of the same method can be implemented in a different way

Copy the Code code as follows:
<?php
Class magic{
The ' $name ' argument is the name of the method to invoke. The ' $arguments ' argument is an enumerated array,
Contains the arguments to pass to the method ' $name '.
Public Function __call ($name, $arguments) {
The implode () function combines array elements into a single string. Implode (Separator,array)
echo "Calling". $name. "With Param:". Implode (",", $arguments). " \ n ";
}
}
$obj = new Magic ();
$obj->run ("Para1", "Para2");//obj to call the Run method, output: Calling run with Param:para1, PARA2
?>

__get () and __set ()

__set () is called when a value is assigned to an unreachable property
When reading the value of an inaccessible property, __get () is called

Copy the Code code as follows:
<?php
Class magic{
Static keyword is needed before function
Public Function __get ($name) {
Return "Getting the property". $name;
}
}
$obj = new Magic ();
echo $obj->classname. " \ n ";//getting the property ClassName
?>

When reading the value of an inaccessible property, __get () is called

Copy the Code code 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 on an inaccessible property, __isset () is called
When Unset () is called on a non-accessible property, __unset () is called

Copy the Code code 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 the 8 PHP object-oriented Magic method of introduction and examples, I hope to be helpful to everyone

http://www.bkjia.com/PHPjc/909341.html www.bkjia.com true http://www.bkjia.com/PHPjc/909341.html techarticle PHP Magic method in detail, PHP magic Detailed from PHP 5 later, the class in PHP can use magic method. The rules that begin with two underscores (__) are preserved as magic methods ...

  • 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.