Talking about the magic methods in Object-Oriented OOP in PHP, object-oriented oop

Source: Internet
Author: User

Talking about the magic methods in Object-Oriented OOP in PHP, object-oriented oop

1. What is a magic method:

PHP provides us with a series of functions starting with _. These functions are automatically called at the right time without manual calls. These functions are called magic functions.
For example:

function __construct(){}This function is automatically called when a new object is created.

2. There are magic methods in PHP and their functions:

1. _ construct (): constructor, which is automatically called when a new object is created.

Eg:

class Person{    public $name;    public $age;        function __construct($name,$age){      $this->name=$name;      $this->age=$age;    }}

When a new Person object is added, the _ construct () function is automatically called to assign the passed parameters to $ name and $ age respectively.

2. _ destruct (): destructor, which is automatically called before an object is destroyed.

Class Person {public $ name; public $ age; function _ destruct () {echo "this object has been destroyed ";}}

As described above, this magic method is executed when an object is destroyed.

3. _ get (): it is automatically called when the private attribute in the category class is passed. The read attribute name is passed and $ this-> attribute name is returned.

class Person{    private $name;    public $age;        function __get($name){     return $this->$name;    }}

As in the code above, $ name cannot be read directly by $ this-> name; after it is set as a private property. Therefore, you need to set the _ get () magic method to read it.

4. _ set (): it is automatically called when a private attribute of the class is assigned, and the attribute name and attribute value to be set are passed.

class Person{    private $name;    public $age;        function __set($key,$value){      $this->$key=$value;    }}

The same as the _ get () method. Private attributes cannot be directly set in the format of $ this-> name = "aaa";. The _ set () method must be set, the input parameters are attribute names and attribute values respectively.

5. _ isset (): this API is called when an isset is used to detect the private attributes of an object. It passes the detection attribute name and returns the isset ($ this-> attribute name)

class Person{    private $name;    public $age;        function __isset($name){    return isset($this->$name);  }}

Isset is called when detecting private attributes of objects.

6. _ unset (): it is called when the unset () function is used to delete the private property of an object. The deleted property name is passed and the unset ($ this-> attribute name) is executed in the method)

class Person{    private $name;    public $age;        function __unset($name){      unset($this->$name);    }}

Same as above, called when the unset () function is used to delete a private attribute.

7. _ toString (): it is called when an echo is used to print an object. The returned content must be a string.

Class Person {public $ name; public $ age; function _ toString () {$ str = <str what you want to print is: <br >\$ name =>{$ this-> name };< br >\$ age =>{$ this-> age };< br> str; return $ str ;}}

It is called when an echo is used to print an object, and the content to be displayed when the object is printed is returned.

8. _ call (): automatically calls an undefined or undisclosed method in a class, passing the called function name and parameter list

Class Person {public $ name; public $ age; function _ call ($ funcName, $ funcParams) {echo "functions you call {$ funcName}, parameter list "; print_r ($ funcParams); echo "nonexistent/undisclosed" ;}}$ zhangsan = new Person (); $ zhangsan-> say (1, 2, 3 );

As shown in the Code, when you call a non-existent say (, 3 );,

The method of the function you call. The parameter list Array ([0] => 123) does not exist.

9. _ clone (): this function is automatically called when an object is cloned using the clone keyword. It is used to initialize and assign values to the newly cloned object.

class Person{    public $name;    public $age;        function __clone(){    $this->name="lisi";    }}  $zhangsan=new Person("zhangsan",24);$lisi=clone $zhangsan;echo $lisi->name;

The printed result is "lisi" instead of "zhangsan ".

10. _ sleep (): automatically called during Object serialization. An array is returned. The values in the array are serializable attributes.

class Person{    public $name;    public $age;       function __sleep(){    return array("name","age");  }}

As shown in the preceding Code, only the "name" and "age" attributes are serialized when serialize () is used to serialize objects.

11. _ wakeup (): it is automatically called during object deserialization and is assigned an initialization value for the newly generated object in deserialization.

class Person{    public $name;    public $age;       function __wakeup(){    $this -> name = "lisi";  }}

Serialize the object before deserialization, and assign the initial value of the object's name attribute to "lisi ".

12. _ autolode (): A function must be declared outside the class. It is called automatically when an undeclared class is instantiated and the instantiated class name is passed. The corresponding class file can be automatically loaded using the class name.

function __autoload($className){  include "class/".strtolower($className).".class.php";}  $zhangsan=new Person();$zhangsan->say();

For example, if the Person class does not exist, call the _ autolode () method to load the class written outside the file, "class /". strtolower ($ className ). ". class. php "; is the relative path of the class file, $ className is

The class name is not in this file. Therefore, when creating an external class file, the naming method must be lower-case class name. class. php. The _ autolode () method is also the only magic method that does not need to be written in the class.

The above is the magic method in PHP, which can be used properly to easily implement various object operations. I hope it will be helpful for everyone's learning, and I hope you can support the house of helping customers more.

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.