Use of php magic method (summary)

The php magic method is summarized as follows:

__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr


1 .__ call () method:

 It is a magic method in PHP. When you call a method in a class and the method does not exist, it will automatically call __call ();

Parameter description: __call ($ name, $ parm) The first parameter: the name of the calling method; the second parameter: the data passed by the calling method (output in the form of an array).

2. __get and __set methods:

These two methods are designed for properties that are not declared in the class and their parent class (no declaration here includes properties that are controlled and private when the object is called (that is, properties that do not have permission to access)) :

__get ($ property) access this method when calling an undefined property__set
($ property, $ value) is called when assigning a value to an undefined property

The advantage of using the magic method is that the access to member attributes can achieve a single entry. For example, when we set the value of an attribute, we need to do some other preliminary work (validation, filtering, or access to a private attribute in the form of an array, etc.). If there is only one entry (__set ()) for setting the attribute, we will You need to add these codes to this entry. In the same way, if we need to modify it later, we only need to modify this one.

3. The __isset and __unset methods:

__isset ($ property) This method is called when the isset () function is called
on an undefined property__unset ($ property) This method and the __get method and _ are called when the unset () function is called on an undefined property
The _set method is the same, no declaration here includes when the object is called, the access control is a protected, private attribute (that is, an attribute that does not have permission to access)

4. The
__autoload method: The __autoload function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has a last chance to load the required classes before PHP fails.
Note: The exception thrown in the __autoload function cannot be caught by the catch block and cause a fatal error.

5,
__construct , __destruct __construct constructor, this method is called when an object is created, the advantage of using this method is: you can make the constructor has a unique name, no matter what the name of the class it is in. So you are changing the class When the name of the name, you do n’t need to change the name of the constructor
__destruct Destructor method, PHP will call this method before the object is destroyed (that is, before it is cleared from memory)
By default, PHP only releases the memory occupied by the object properties and destroys Object-related resources. The
destructor allows you to execute arbitrary code to clear memory after using an object.
When PHP determines that your script is no longer related to the object, the destructor will be called.
Within a function's namespace This happens when the function returns.
For global variables, this happens at the end of the script. If you want to destroy an object explicitly, you can assign any other value to the variable that points to the object. Usually assign the variable to NULL Or call unset.

6.
The object assignment in __clone PHP5 is the reference assignment used. If you want to copy an object, you need to use the clone method. When you call this method, the object will automatically call the __clone magic method.
If you need to perform some initialization operations during object copying, you can Implemented in __clone method

7. The 
__toString __toString method is automatically called when an object is converted into a string. For example,
if you use echo to print an object, if the class does not implement this method, you cannot print the object through echo, otherwise it will display: Catchable fatal error: Object of class test could not be converted to string in
this method must return a string

Prior to PHP 5.2.0, the __toString method only took effect when combined with echo () or print (). After PHP 5.2.0, it can take effect in any string environment (for example, through printf (), using the% s modifier), but cannot be used in non-string environments (such as using the% d modifier). From PHP 5.2.0, if an object with no __toString method defined is converted to a string, an E_RECOVERABLE_ERROR error will be reported.

8、__sleep、__wakeup
__sleep 串行化的时候用
__wakeup 反串行化的时候调用
serialize() 检查类中是否有魔术名称 __sleep 的函数。如果这样,该函数将在任何序列化之前运行。它可以清除对象并应该返回一个包含有该对象中应被序列化的所有变量名的数组。
使用 __sleep 的目的是关闭对象可能具有的任何数据库连接,提交等待中的数据或进行类似的清除任务。此外,如果有非常大的对象而并不需要完全储存下来时此函数也很有用。
相反地,unserialize() 检查具有魔术名称 __wakeup 的函数的存在。如果存在,此函数可以重建对象可能具有的任何资源。
使用 __wakeup 的目的是重建在序列化中可能丢失的任何数据库连接以及处理其它重新初始化的任务。

9、__set_state
当调用var_export()时,这个静态 方法会被调用(自PHP 5.1.0起有效)。
本方法的唯一参数是一个数组,其中包含按array(’property’ => value, …)格式排列的类属性。

10、__invoke
当尝试以调用函数的方式调用一个对象时,__invoke 方法会被自动调用。
PHP5.3.0以上版本有效


11、__callStatic
它的工作方式类似于 __call() 魔术方法,__callStatic() 是为了处理静态方法调用,
PHP5.3.0以上版本有效
PHP 确实加强了对 __callStatic() 方法的定义;它必须是公共的,并且必须被声明为静态的。同样,__call() 魔术方法必须被定义为公共的,所有其他魔术方法都必须如此



发布了14 篇原创文章 · 获赞 3 · 访问量 2029

Guess you like

Origin blog.csdn.net/energy_tank/article/details/46563701