PHP 5.3 New Magic Method __invoke Overview _php Tips

Source: Internet
Author: User
Tags modifier php error php example serialization

PHP has added a magic method called __invoke since version 5.3, which allows you to invoke the object directly after creating the instance. The following example shows:

Class TestClass
{public
function __invoke
{
print ' Hello World ';
}
}
$n = new TestClass;
$n ();

The results of the execution are:

Hello world.

The official PHP example is as follows:

Class Callableclass
{public
function __invoke ($x)
{
var_dump ($x);
}
}
$obj = new Callableclass;
$obj (5);
Var_dump (Is_callable ($obj));

Attached:php Other magic methods:

You can use the Magic method for classes in PHP. The rules that start with two underscores (__) are reserved as magic methods, so it is recommended that you do not start with a function name unless you are trying to overload an existing magic method.

The function names __construct, __destruct, __call, __callstatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toS Tring, __invoke, __set_state and__clone are magical in PHP classes. You are cannot have functions with this names in any of your classes unless your want the magic functionality with them.

The following two methods are designed for properties that are not declared in the class and their parent class.
__get ($property) When an undefined property is invoked, this method is triggered and the parameter passed is the name of the property being accessed.
when __set ($property, $value) assigns a value to an undefined property, this method is triggered and the passed parameter is the property name and value that is set.
the absence of a declaration here includes a property (that is, a property that has no access) when an object invocation is used, and the access control is proteced,private.
As with the __get method and the __set method, there is no declaration that includes properties that have access control as proteced,private when using object invocation (that is, properties that do not have permission to access).

Undefined methods here include methods that do not have access to permissions; If the method does not exist, find this method in the parent class, and if the parent class does not exist, call the __call () method of this class, and if there is no __call () method in this class, find the __call () method in the parent class .

The __autoload function , which is invoked automatically when an attempt is made to use a class that has not 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: The exception thrown in the __autoload function cannot be caught by a catch statement block and can cause a fatal error, so it should be caught in the function itself.

__construct constructs a method that calls this method when an object is created, and the benefit of using this method relative to PhP4 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're changing the name of the class, You do not need to change the name of the constructor method.

__destruct destructor , PHP calls this method before the object is destroyed (that is, before it is purged from memory). By default, PHP releases only the memory occupied by the object's properties and destroys the object-related resources, and the destructor allows you to execute arbitrary code to erase memory 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. You typically assign a variable to null or call unset.

An object assignment in PHP5 is a reference assignment that is used, and if you want to copy an object, you need to use the Clone method, and calling this method is an object that automatically invokes the __clone magic method, which you can implement in the __clone method if you need to perform some initialization operations on the object copy.

The __tostring method is invoked automatically when an object is converted to a string, such as when the object is printed with Echo.

Before php5.2.0, the __tostring method takes effect only if echo () or print () is used in conjunction. After php5.2.0, it can take effect 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). From php5.2.0, if you convert an object that does not define a __tostring method to a string, a e_recoverable_error error is reported.

__wakeup Drag when the row is called

Serialize () checks whether the class has a magic name __sleep function. If so, the function will run before any serialization. It can clear the object and should return an array that contains all the variable names that should be serialized in the object.

The purpose of using __sleep is to turn off any database connections that the object might have, commit the waiting data, or perform similar cleanup tasks. Also, this function is useful if you have a very large object and you do not need to store it completely.

PHP Conversely, unserialize () checks the existence of a function that has a magic name __wakeup. If present, this function can reconstruct any resources that the 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.
The __invoke method is invoked automatically when an attempt is made to call an object in the form of a call to a function.

It works like the __call () Magic Method, __callstatic () to handle static method calls.

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.

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.