The Magic We missed in those years (Magic Methods)

  • 2020-12-13 18:51:38
  • OfStack

One advantage of PHP objects is the ability to use magic methods that override the default behavior of a class without modifying external code, which makes the PHP syntax less redundant and more extensible. These methods are easy to identify, and they all begin with a double underline (__).
Every 4en (), destruct (), callStatic (), get (), isset (), unset (), wakeup (), toString (), 16en_ES17en (), clone (), and more are called the "magic method" (Magic methods) in PHP. You cannot use method names when naming your own class methods unless you want to use their magic functions.

Note:

PHP leaves all class methods that begin with __ (2 underscores) as magic methods. So in addition to the magic method above, it is recommended not to prefix the __ method with the __ method of a class.

, 29en, 30en

These two methods are designed for properties that are not declared in the class and their parent classes.


__get(string $name)  // When calling 1 Access this method when there are no defined properties; 
__set(string $name, mixed $value)  // to 1 Called when assigning an undefined property; 

The lack of a declaration here includes properties that have access control of proteced, private, when invoked with an object (that is, properties that do not have permission to access).

, 42en, 43en


__isset( $property ) // When in 1 Called on an undefined property isset() Call this method when function; 
__unset( $property ) // When in 1 Called on an undefined property unset() Call this method when function; 

___, 50en

 __call(string $name, array $arguments) // When calling 1 The undefined method is called this method.  

The undefined methods here include methods that have no permission to access them.

 __callStatic(string $name, array $arguments) 

Each ___ 61en () is called when an unreachable method (such as undefined or invisible) is called in a static method.

SucccallStatic works like the magic method with ___ 65en (), with SUCCcallStatic () to handle static method calls, effective with PHP5.3.0 and above.
PHP does strengthen the definition of the method with succcallStatic (); It must be public, and it must be declared static. Also, the magic method with succcall () must be defined as common, as must all the other magic methods.

4, __autoload

The function with SUCCautoload is called automatically if you try to use a class that has not been defined. By calling this function, the script engine has one last chance to load the required classes before THE PHP error fails.
Note: The exception thrown in the function with succautoload cannot be caught by the catch statement block and result in a fatal error.


, 85en, 86en

___, which is called when an object is created.
The advantage of using this method is that you can make the constructor have a unique 1 without 2 name, regardless of the name of the class in which it is located. So when you change the name of the class, you don't have to change the constructor name.
___, destruct destruct method, which IS called by PHP before the object is destroyed (that is, cleared from memory).
By default, PHP simply frees the memory occupied by object properties and destroys the object-related resources.
Destructors allow you to execute any code to clear memory after using 1 object.
When PHP decides that your script is no longer object-dependent, the destructor is called.
In the namespace of 1 function, this happens when the function return.
For global variables, this happens 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 to NULL or calling unset.

6, __clone

The object assignment in PHP5 is the reference assignment used, and if you want to copy 1 object you need to use the clone method, which on calling it will automatically call the magic method with SUCCclone.
If you need to do some initialization in object replication, you can do it with the method ___ 113en.

7, __toString


public string __toString ( void )

The method with SUCCtoString is called automatically when you turn an object into a string, such as with echo printing the object. What should it show? This method must return a string, or it will issue a fatal error at the E_RECOVERABLE_ERROR level.


$myObject = new myClass();
echo $myObject;
// Will look for a magic method echo
$myObject->__toString();

Note: You cannot throw an exception in the method with SUCCtoString (). Doing so can lead to a fatal error.

It should be noted that, before PHP 5.2.0, the method of SUCCessie 136en () will only come into effect if used directly with echo or print. After PHP 5.2.0, it works in any string environment (for example, through printf(), using the %s modifier), but not in a non-string environment (such as using the %d modifier). Starting with PHP 5.2.0, if you convert an object with an undefined succtoString () method to a string, you get an E_RECOVERABLE_ERROR level error.

, ___ 150en () and ___ 151en ()


public array __sleep ( void )
void __wakeup ( void )

The serialize() function checks if there is a magic method with each class/ES158en (). If it exists, the method is called before the serialization operation is performed. This function can be used to clean up an object and return an array containing the names of all the variables in the object that should be serialized. If the method returns nothing, NULL is serialized and produces an E_NOTICE level error.

Note:

___ 166en () cannot return the name of the private member of the parent class, and doing so will result in an ERROR of the E_NOTICE level. You can use the Serializable interface instead.
The method with SUCCsleep () is often used to submit uncommitted data, or similar cleanup operations. At the same time, if you have 1 large object, but you don't need to save all of it, this feature works well.
By contrast, unserialize() checks if there is a method with 1 per ___ 174en (). If it does, the method with SUCCwakeup is called first, pre-preparing the resources needed by the object.
Succwakeup () is often used with deserialization operations, such as reestablishing a database connection, or performing other initialization operations.

9, __invoke ()

mixed __invoke ([ $... ] ) 

The method of ___ 186en () is automatically called when you try to call an object as a calling function.

10, __set_state ()

static object __set_state ( array $properties ) 

Since PHP 5.1.0, this static method is called when the var_export() export class is called.
The only parameter 1 of this method is an array containing press array('property' = > value, ...) Formatting class attributes.


Related articles: