The Magic method in object-oriented oop in PHP

Source: Internet
Author: User
This article mainly introduces the method of magic in object-oriented oop in PHP, which has certain reference value, and the interested partners can refer to

One, what is the Magic method:

PHP provides us with a series of functions with the beginning, which do not need to be called manually, and will be called automatically at the appropriate time, such functions are called magic functions.
For example:

function construct(){}This function is called automatically when new object is newly

In PHP, there are magic methods and their effects:

1.construct (): constructor, automatically called when new object

eg

Class person{public    $name;    public $age;        function construct ($name, $age) {      $this->name= $name;      $this->age= $age;    }}

The construct () function is automatically called when the new Person object is newly, assigning the passed arguments to $name, $age.

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

Class person{public    $name;    public $age;        function Destruct () {    echo "This object has been destroyed";  }}

As mentioned above, this magic method is executed when the object is destroyed

3.get (): Automatically called when accessing a private property in a class, passing the Read property name, returning the $this-> property name

Class person{    private $name;    public $age;        function Get ($name) {     return $this, $name;    }}

As in the code, the $name is set as a private property and cannot be read directly by $this->name;, so the Get () Magic method needs to be set to read.

4.set (): Automatically called when assigning a value to a private property of a class, passing property names to be set, property values

Class person{    private $name;    public $age;        function set ($key, $value) {      $this, $key = $value;    }}

With the Get () method, the private property cannot be set directly by $this->name= "AAA", the Set () method needs to be setup, and the passed parameters are the property name and the attribute value, respectively.

5.isset (): Called when detecting private properties of an object using Isset, passing the detected property name, returning Isset ($this property name)

Class person{    private $name;    public $age;        function Isset ($name) {    return isset ($this, $name);}  }

Called when using Isset to detect the private properties of an object, as in the other.

6.unset (): Called when an object private property is deleted using the unset () function, passing the deleted property name, executing unset ($this-property name) in the method

Class person{    private $name;    public $age;        function unset ($name) {      unset ($this, $name);}    }

Ditto, called when a private property is removed with the unset () function.

7.toString (): Called When using echo to print an object, returns the content to be displayed when the object is printed, returns a string

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

Called when you print an object with Echo, and returns what you want to display when you print the object.

8.call (): called automatically when calling a method that is not defined or exposed in a class, passing the called function name and argument list

Class person{public    $name;    public $age;        function call ($funcName, $funcParams) {      echo "you are calling functions {$funcName}, parameter list";      Print_r ($funcParams);      echo "does not exist/is not public";    }} $zhangsan =new person (); $zhangsan->say (a);

As shown in the code, the call does not exist for say (.

You call the function of the method, the parameter list array ([0] = 123) does not exist

9.clone (): When cloning an object using the Clone keyword, it is called automatically to initialize the assigned value for the newly cloned object

Class person{public    $name;    public $age;        function Clone () {    $this->name= "Lisi";    }}  $zhangsan =new person ("Zhangsan"), $lisi =clone $zhangsan, Echo $lisi->name;

The printed result is "Lisi", not "Zhangsan".

10.sleep (): Automatically called when the object is serialized, returns an array where the values in the array are the properties that can be serialized

Class person{public    $name;    public $age;       function sleep () {    return Array ("name", "Age");}  }

As in the above code, only "name" is used when serializing an object with serialize (), and the "age" property is serialized.

11.wakeup (): Automatic invocation of object deserialization, initialization assignment for deserialization of newly generated objects

Class person{public    $name;    public $age;       function Wakeup () {    $this, name = "Lisi";  }}

Serializes the object and then deserializes it, assigning an initial value of "Lisi" to the object's Name property.

12.autolode (): You need to declare a function outside the class, automatically call when an undeclared class is instantiated, pass the instantiated class name, and automatically load the corresponding class file using the class name.

function AutoLoad ($className) {  include "class/". Strtolower ($className). ". Class.php ";}  $zhangsan =new person (); $zhangsan->say ();

As the above code, the person class does not exist when calling the Autolode () method, loading a class written outside the file, "class/". Strtolower ($className). Class.php "; Is the relative path to the class file, $className is

The class name of the class that is not in this file, so the naming method must be a lowercase class name when creating an external class file. class.php. The Autolode () method is also the only one by one magic methods that are not written in the class.

Related Article

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.