The Magic methods we missed in those years

Source: Internet
Author: User
Tags mixed modifier php error

One advantage of the

 php object is that you can use magic methods that override the default behavior of a class without having to modify the external code, which makes the PHP syntax less redundant and more extensible. These methods are well identified, and they all start with a double underline (__).

One advantage of PHP objects is that you can use magic methods that override the default behavior of a class without having to modify the external code, which makes the PHP syntax less redundant and more extensible. These methods are well identified and they all start with a double underline (__). For example: __construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __isset (), __unset (), __sleep (), __wakeup () , __tostring (), __invoke (), __set_state (), and __clone () are called "Magic Methods" (Magic methods) in PHP. You cannot use these method names when naming your own class methods, unless you want to use their magic features.   NOTE:   PHP keeps all class methods that begin with __ (two underscores) as magic methods. Therefore, in addition to the above magic method, it is recommended that you do not prefix the class method with __.   1, __get, __set   These two methods are designed for properties that are not declared in the class and their parent class.     Code as follows: __get (string $name)  //Access this method when an undefined property is invoked; __set (string $name, mixed $value)  // Called when assigning a value to an undefined property;     There is no declaration here including properties that have access control as proteced,private when using object invocation (that is, properties that do not have permission to access).   2, __isset, __unset     code as follows: __isset ($property)//Call this method when calling the Isset () function on an undefined property; __unset ($property)//when Call this method when calling the Unset () function on an undefined property;     3, __call, __callstatic       code as follows: __call (string $name, array $ arguments)//When an undefined method is invoked, this method is called.     The undefined method here includes methods that do not have permission access.     Code as follows: __callstaTic (string $name, array $arguments)     When an inaccessible method (such as undefined or invisible) is invoked in a static method, __callstatic () is invoked.   __callstatic It works like the __call () Magic Method, __callstatic () is to handle static method calls, PHP5.3.0 the above version is valid. PHP does strengthen the definition of the __callstatic () method, it must be public and must be declared static. Similarly, the __call () Magic method must be defined as public, and all other magic methods must be so. The   4, __autoload   __autoload function, which is invoked automatically when an attempt is made to use a class that has not yet been defined. By calling this function, the script engine had the last chance to load the class that was required before the PHP error failed. Note: Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error.     5, __construct, __destruct   __construct construction method, which is called when an object is created. The advantage of using this method is that you can make the construction method have a unique name, regardless of the name of the class in which it resides. So you don't need to change the name of the constructor when you change the name of the class. __destruct destructor, PHP calls this method before the object is destroyed (that is, before it is purged from memory). By default, PHP only frees the memory occupied by object properties and destroys object-related resources. Destructors allow you to clean up memory by executing arbitrary code after using an object. Destructors are invoked when PHP determines that your script is no longer related to objects. Within the namespace of a function, this occurs when the function return. For global variables, this occurs at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable that points to the object, usually assigning the variable on duty to null or calling unset. The object assignment in   6, __clone   PHP5 is a reference assignment that is used, and if you want to copy an object, you need to use the Clone method, which automatically invokes the __clone magic method when this method is called. If you need to perform some initialization operations on object replication, you can implement it in the __clone method.   7, __tostring      code as follows: public The string __tostring (void)     __tostring method is invoked automatically when an object is converted to a string, such as when an object is printed with Echo. What should be shown. This method must return a string, or it will emit a fatal error at the e_recoverable_error level.       Code as follows: $myObject = new MyClass (); Echo $myObject; 'll look for a magic method echo $myObject->__tostring ();     NOTE: You cannot throw an exception in the __tostring () method. Doing so can lead to fatal errors.   It should be noted that before PHP 5.2.0, the __tostring () method takes effect only if it is used directly with Echo or print. After PHP 5.2.0, it can take effect in any string environment (for example, by printf (), using the%s modifier) but not in a non-string environment (such as using the%d modifier). From PHP 5.2.0, if you convert an object that does not define a __tostring () method to a string, a E_recoverable_error level error is generated.   8, __sleep () and __wakeup ()     code are as follows: public array __sleep (void) void __wakeup (void)     Seri The Alize () function checks whether a magic method __sleep () exists in the class. If present, the method is invoked before the serialization operation is performed. This feature can be used to clean up objects and return an array containing all the names of the variables that should be serialized in the object. If the method does not return any content, NULL is serialized and a E_notice level error is generated.   Note:   __sleep () cannot return the name of the private member of the parent class, which results in a e_notice level error. You can use the Serializable interface instead. The __sleep () method is often used to submit uncommitted data, or similar cleanup operations. At the same time, if there are some large objects, but do not need to save all, this function is very useful. On the contrary, UnserializE () checks for the existence of a __wakeup () method. If present, the __wakeup method is called first, and the resource required by the object is prepared beforehand. __wakeup () is often used in deserialization operations, such as the re-establishing a database connection, or performing other initialization operations.   9, __invoke ()       code is as follows: Mixed __invoke ([$ ...])     The __invoke () method is invoked automatically when an attempt is made to call an object in the form of a call to a function.   10, __set_state ()     code as follows: Static object __set_state (array $properties)     from PHP 5.1.0 when calling V Ar_export () This static method is invoked when the class is exported. The unique parameter of this method is an array that contains class attributes arranged by array (' property ' => value, ...).  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn 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: and provide relevant evidence. A staff member will contact you within 5 working days.