PHP Magic method Detailed _php skills

Source: Internet
Author: User

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

__tostring () and __invoke ()

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

Copy Code code as follows:

<?php
Class magic{
Public Function __tostring () {
Return "Hello world!";
}
}
$obj = new Magic ();
echo $obj;//hello world!
?>

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

Copy 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 (a)//__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 invoked

__callstatic (): When an object accesses a static method name that does not exist, the __callstatic () method is automatically invoked

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

Copy Code code as follows:

<?php
Class magic{
The ' $name ' parameter 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 invoke the Run method, output: Calling run with Param:para1, PARA2
?>

__get () and __set ()

When you assign a value to an inaccessible property, __set () is called
When reading the value of an inaccessible property, __get () is called

Copy Code code as follows:

<?php
Class magic{
A function must have a static keyword before it
Public Function __get ($name) {
Return "getting". $name;
}
}
$obj = new Magic ();
echo $obj->classname. " \ n ";//getting the property ClassName
?>

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

Copy 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 invoked on an inaccessible property, __isset () is called
When unset () is invoked on an inaccessible property, __unset () is called

Copy 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 Introduction and examples, I hope to be helpful to everyone

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.