PHP Magic Method Summary
From the later version of PHP5, the PHP class will be able to use the Magic method.
PHP rules to start with two underscore (__) methods are preserved as a magic method, it is recommended that the function name is best not to start without __, unless it is to reload the existing magic method.
The Magic Methods in PHP are: __construct, __destruct, __call, __callstatic,__get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state, __clone, __autoload
1, __get, __set
These two methods are designed for properties that are not declared in the class and their parent class.
__get ($property) This method is triggered when an undefined property is called, and the passed parameter is the property name that is accessed
__set ($property, $value) when assigning a value to an undefined property, this method is triggered, and the passed parameter is the property name and value that is set
There is no declaration here that the access control is a Proteced,private property (that is, a property without permission access) when using object invocation.
2, __isset, __unset
__isset ($property) This method is called when the Isset () function is called on an undefined property
__unset ($property) This method is called when the unset () function is called on an undefined property
As with the __get method and the __set method, there is no declaration here that the access control is a property of Proteced,private when using an object invocation (that is, a property without permission access)
3, __call
__call ($method, $arg _array) when calling an undefined method is to call this method
The undefined method here includes a method that does not have access , if the method does not exist in the parent class to find this method, if it does not exist in the parent class to call the __call () party of this class, if there is no __call () method in this class to find the __call () method in the parent class
4, __autoload
The __autoload function, which is called automatically when trying to use a class that has not yet been defined. By calling this function, the scripting engine has the last chance to load the required classes before PHP fails.
If you are defining a global auto-load class, you must register the processing class with the PHP standard library using the Spl_autoload_register () method:
?
View Plain Copy to Clipboard Print ?
-
- class Loader??
- {??
- static function autoload_class ($class _name)??
- {??
- //Looking for the right $class_name class, and introducing, no then throwing an exception ??
- }??
- }??
- ??
- /**
- set automatic loading of objects
- *?? spl_autoload_register―register given function as __autoload () implementation
- */??
- spl_autoload_register (array(' Loader ', ' Autoload_class '));??
- ??
- $a = new Test (); //test useless require on the instantiation, to achieve automatic loading, many frameworks use this method to automatically load class ??
- ??
- ???
Note: Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error, so the function itself should be captured.
5, __construct, __destruct
__construct constructs a method that calls this method when an object is created, and the advantage of using this method with respect to PHP4 is that the constructor can 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 simply frees the memory occupied by the object's properties and destroys the object-related resources, and the destructor allows you to execute arbitrary code to clear the memory after using an object. When PHP decides that your script is no longer related to the object, the destructor is called.
Within the namespace of a function, this occurs at the time of 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. You typically assign a variable to be on duty to null or call unset.
6, __clone
The object assignment in PHP5 is to use a reference assignment, and if you want to copy an object then you need to use the Clone method, which is called by the object to call the __clone Magic method automatically, if the object replication requires some initialization to be performed, it can be implemented in the __clone method.
7, __tostring
The __tostring method is called automatically when an object is converted to a string, such as when using Echo to print an object.
If the class does not implement this method, the object cannot be printed through ECHO, otherwise it will be displayed: Catchable fatal Error:object of class test could not being converted to string in
This method must return a string.
Before PHP 5.2.0, the __tostring method will only take effect if you use Echo () or print () together. After PHP 5.2.0, you can take effect in any string environment (for example, with the%s modifier through printf (), but not in a non-string environment (such as using the%d modifier). From PHP 5.2.0, a e_recoverable_error error is reported if an object with an undefined __tostring method is converted to a string.
8, __sleep, __wakeup
__sleep serialization is used when
__wakeup Crossdress is called when the line is being serialized
Serialize () checks the class for a function that has the magic name __sleep. If so, the function will run before any serialization. It clears the object and should return an array containing all the variable names that should be serialized in the object.
The purpose of using __sleep is to close any database connections that an object might have, submit data for waiting, or perform similar cleanup tasks. In addition, this function is useful if you have very large objects and do not need to be fully stored.
Conversely, unserialize () checks for the existence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that an object might have.
The purpose of using __wakeup is to reconstruct any database connections that may be lost in serialization and to handle other reinitialization tasks.
9, __set_state
This static method is called when Var_export () is called (Valid from PHP 5.1.0).
The only parameter to this method is an array that contains the by array (' property ' = = value, ...) The class properties of the format arrangement.
10, __invoke
The __invoke method is called automatically when an attempt is made to invoke an object in a way that invokes a function.
PHP5.3.0 above version Valid
11, __callstatic
It works like the __call () Magic Method, __callstatic () is to handle static method calls,
PHP5.3.0 above version 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
====================================================================
PHP put all the __ (two underscores) the class method at the beginning as a magic method. So when you define your own class method, do not prefix __ .
?
1 , __construct ()
When an object is instantiated, the method of constructing the object is called first;
We know php5 The object model and the same class name function are constructors for the class, so if you define both the constructor and the __construc () method, php5 is called by default __contruct () the same name function is not called, so __contruct () as the default constructor for a class;
?
2 , __destruct ()
Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.
?
3 , __get (string $name)
called when attempting to read a property that does not exist, or if an attempt is made to read a property that does not exist for an object, PHP will give you the wrong information. If you add the __get method to the class , and we can use this function to implement a variety of actions like reflection in Java .
?
4 , __set (String $name, mixed $value)
Called when assigning values to undefined variables
?
5 , __call (string $name, array $arguments)
when a non-accessible method (such as undefined or invisible) is invoked, the __call () will be called.
__callstatic (string $name, array $arguments)
when a non-accessible method (such as undefined or invisible) is invoked in a static method, the __callstatic () will be called.
?
6 , __tostring ()
called when an object is printed, this method is similar to Java of the toString method, and callback the function when we print the object directly.
?
7 , __clone ()
Called when an object is cloned.
?
8 , __sleep ()
Serialize () The function checks to see if there is a magic method __sleep. if present, __sleep () method is called first, then the serialization operation is performed. This feature can be used to clean up an object and return an array that contains all the variable names in the object. If the method does not return anything, then NULL is serialized, resulting in a e_notice error. The __sleep method is often used to commit uncommitted data, or similar operations. At the same time, if you have some very large objects, do not need to save, this function is very useful.
?
9 , __wakeup ()
with the __sleep () instead, unserialize () checks if there is a __wakeup method. If present, the __wakeup method is called before the object data is prepared beforehand. __wakeup are often used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.
Ten , __isset ()
when a variable is called on an undefined isset () or empty () , __isset () is called.
?
One , __unset ()
unset is called when a property of an object is invoked. such as:unset ($c->name).
?
A , __set_state ()
called Var_export called when it is invoked. Use The return value of __set_state as the return value of var_export .
?
- , __autoload ()
when an object is instantiated, the method is called if the corresponding class does not exist. Simply speaking, the automatic loading of classes ; when you try to use a PHP classes that are not organized , It will look for a __autoload the global function . If this function is present , PHP it will be called with a parameter. , parameter is the name of the class.
?
- , __invoke ()
When attempting to invoke an object in a way called a function, the __invoke method is automatically called.
?
Magic Constants :
__line__ the current line number in the file. ?
__file__ the full path and file name of the file. If used in the included file, returns the file name that is included. from PHP 4.0.2 onwards,__file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path. ?
__dir__ the directory where the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to dirname (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New inPHP 5.3.0 ) =?
__function__ Function Name ( PHP 4.3.0 new Plus). from PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4 , This value is always in lowercase letters. ?
__class__ the name of the class ( PHP 4.3.0 new Plus). from PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4 , This value is always in lowercase letters. ?
__method__ the method name of the class ( PHP 5.0.0 new Plus). Returns the name of the method when it is defined (case-sensitive). ?
__namespace__ the name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new)?