Summary of magic methods in PHP: __construct ,__ destruct ,__ call ,__ callstatic ,__ get ,__ set ,__ isset ,__ unset ,__ sleep ,__ wakeu

Source: Internet
Author: User
Tags autoload pear php error

Summary of magic methods in PHP: __construct, _ destruct, _ call, _ callstatic, __get, _ set, _ isset, _ unset, _ sleep, _ wakeup, _ tostring, _ set_state, _ clone and _ autoload

1. _ Get, _ set
These two methods are designed for attributes that are not declared in the class and their parent class.
_ Get ($ property) access this method when an undefined property is called
_ Set ($ property, $ value) is called when an undefined property is assigned a value.
There is no declaration here: when an object is called, the access control is "proteced" and "private" (that is, the attribute with no access permission)

2. _ isset and _ 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.
Similar to the _ Get method and _ set method, there is no declaration here, including the access control for proteced and private attributes when an object is called (that is, attributes with no access permission)
3. _ call
_ Call ($ method, $ arg_array) When an undefined method is called
The undefined method here includes methods with no access permission.

4. _ autoload
_ 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 the last chance to load the required class before a PHP error fails.
Note: The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.

5. _ construct and _ destruct
_ Construct constructor: This method is called when an object is created. The advantage of this method is that the constructor has a unique name, no matter what the name of its class is. in this way, you do not need to change the name of the constructor when changing the class name.
_ Destruct destructor. 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 Object Attributes and destroys object-related resources.
The Destructor allows you to execute arbitrary operations after using an object.CodeTo clear the memory.
When PHP decides that your script is no longer related to objects, the Destructor will be called.
In the namespace of a function, this occurs when the function returns.
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 pointing to the object. usually, the variable value is null or unset is called.

6. _ clone
Object assignment in PhP5 is a reference assignment. 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 on Object replication, You can implement them in the _ clone method.

7. _ tostring
The _ tostring method is automatically called when an object is converted to a string. For example, when an object is printed using echo
If the class does not implement this method, the object cannot be printed using echo; otherwise, the following error occurs: catchable fatal error: object of class test cocould not be converted to string in.
This method must return a string

Before PHP 5.2.0, The __tostring method takes effect only when ECHO () or print () is used in combination. PHP 5.2.0 and later can take effect in any string environment (for example, using the % s modifier through printf (), but cannot be used in non-string environment (for example, using the % d modifier ). From PHP 5.2.0, if an undefined _ tostring method object is converted to a string, an e_recoverable_error error is returned.

8. _ sleep and _ Wakeup
_ Used to serialize sleep
_ Called when wakeup is deserialized
Serialize () checks whether the class has a function named _ sleep. In this case, the function will run before any serialization. It can clear the object and should return an array containing all variable names to be serialized in the object.
The purpose of _ sleep is to close any database connection that an object may have, submit data in waiting or perform similar cleanup tasks. In addition, this function is useful if a very large object does not need to be fully stored.
Conversely, unserialize () checks the existence of a function with the magic name _ wakeup. If yes, this function can reconstruct any resources that an object may have.
_ Wakeup is used to reconstruct any database connections that may be lost during serialization and process other re-initialization tasks.

9. _ set_state
When var_export () is called, this static method is called (effective from PhP 5.1.0 ).
The unique parameter of this method is an array, which contains values by Array ('properties' => value ,...) Class attributes in the format.

10. _ invoke
When you try to call an object by calling a function, the __invoke method is automatically called.
Php5.3.0 and later versions are valid.
11. _ callstatic
It works in a way similar to the _ call () magic method, __callstatic () is used to handle static method calls,
Php5.3.0 and later versions are valid.
PHP does enhance the definition of the _ callstatic () method. It must be public and declared as static. Similarly, the __call () magic method must be defined as public, and all other magic methods must be.

 

Author:Fat man

From:Http://www.phppan.com/2010/03/php-magic-method-__construct-__destruct-__call-__callstatic__get-__set-__isset-__unset-__sleep-__wakeup-__tostring-__set_state/

 

The following generations are from:

Http://bbs.phphubei.com/thread-14850-1-1.html

 

Code

<?
/* When you try to call a non-existent or unavailable method in the class, the _ call () parameter in the class must accept two parameters. The first parameter stores the method name, the second parameter stores parameters of non-existent methods (this parameter will be placed in an array with the same name as this parameter) */
Class Callclass
{
Function _ Call ( $ Method_name , $ P )
{
Echo   " Use _ call to try to call a nonexistent/unavailable member method <br> " ;
Echo   $ Method_name ;
Echo   " <PRE> " ;
Print_r ( $ P );
Echo   " </PRE> " ;
}
}
$ OBJ   =   New Callclass ();
$ OBJ -> Method ( 1 , 2 , " Hello " , " HP " )
?>
<?
/* When an attempt is made to call a non-existent or invisible property, the call _ set () must accept two parameters, attribute name and attribute value. */
Class Myshop
{
Private   $ P   =   Array ();
Function _ Set ( $ Name , $ Value )
{
Echo   ' Set: property name: ' . $ Name ;
Echo   ' Set: The property value is: ' . $ Value ; Echo   " <Br> " ;
$ This -> P [ $ Name ] =   $ Value ;
}
Function _ Get ( $ Name ) // Get attribute name
{
Echo   " Get :: $ Name <Br> " ;
Return   Array_key_exists ( $ Name , $ This -> P) ?   $ This -> P [ $ Name ] :   Null ;
}
}
$ Shop   =   New Myshop ();
$ Shop -> Feiker = 2 ; // To access a property that does not exist, call _ set ();
$ Shop -> Pear =   5 ;
$ Shop -> Pear ++ ; // Call _ Get () first, because there is only a name at the beginning, and then call _ set ();
Echo   $ Shop -> Feiker;
Echo   $ Shop -> Pear;
Echo ' Bytes ------------------------------------------------------------------------------------------ ' . " <Br> " ;
?>
<?
// _ Unset () and _ isset ()
Class Person
{ // Private attributes
Private   $ Name , $ Sex , $ Age ;
Function _ Get ( $ Property_name )
{ // Obtain the name of a private property
If ( Isset ( $ Property_name )) Echo   $ This -> $ Property_name ;
}
Function _ Set ( $ Property_name , $ Value )
{
$ This -> $ Property_name   =   $ Value ;
Echo   " Name :{ $ Property_name } Value :{ $ Value } <Br> " ;
}
Function _ Isset ( $ FF )
{ // _ Isset ()
Echo   " The property of a private member measured by the isset () function is automatically called. <br> " ;
Return   Isset ( $ This -> $ FF );
}
Function _ Unset ( $ Kk )
{ // _ Unset ()
Echo   " Unset () is used to automatically call a method to delete private member variables when the class is called externally. <br> " ;
Unset ( $ This -> $ Kk );
}
Function A ()
{
Echo   ' Bb ' . $ This -> Name;
}
}
$ OBJ   =   New Person ();
$ OBJ -> Name =   " Feike " ;
// Isset () is used to determine private member variables.
Isset ( $ OBJ -> Name );
// Automatic Call to delete private member variables
Unset ( $ OBJ -> Name );
Echo   $ OBJ -> A ();
// Echo 'aaa'. $ obj-> name. 'bbb '; // result: Apsara aaabbb
?>

 

 

Http://bbs.phphubei.com/thread-7987-1-1.html

 

PHP programming mode

 

Five common PHP design modes

 

Http://blog.51yip.com/php/947.html

 

Http://zzk.cnblogs.com? W = PHP % 20__ set

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.