Since version 5.3, PHP has added a magic method called __invoke. Using this method, you can directly call the object after creating an instance. As shown in the following example:
class testClass { public function __invoke { print "hello world"; } } $n = new testClass; $n();
The execution result is:
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:Detailed explanation of other magic methods of PHP:
The magic method can be used in the classes in php. It stipulates that methods starting with two underscores (__) are retained as magic methods, so it is recommended that you do not start with __ unless it is to overload existing magic methods.
The function names __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __invoke, __set_state and__clone are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated 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 called, this method will be triggered, and the passed parameter is the property name that is accessed.
When __set($property,$value) assigns a value to an undefined property, this method will be triggered, and the passed parameters are the property name and value set.
The non-declaration here includes properties that are protected, private when invoked using an object (i.e. properties that do not have permission to access).
Same as the __get method and the __set method, the non-declaration here includes the property that is protected, private when called using an object (i.e., property that has no permission to access).
The undefined methods here include methods that do not have permission to access; if the method does not exist, go to the parent class to find this method.If the parent class does not exist, call the __call() method of this class. If the __call() method does not exist in this class, look for the __call() method of this class.。
__autoload function, it will be automatically called when trying to use a class that has not been defined yet. By calling this function, the script engine has the last chance to load the required class before the php error fails.
Notice:Exceptions thrown in the __autoload function cannot be caught by the catch statement block and cause a fatal error, so it should be captured in the function itself.
__construct construct method, when an object is created, the advantage of using this method compared to php4 is that the constructor can have a unique name, regardless of the name of the class it is located in. In this way, when you change the name of the class, you do not need to change the name of the constructor.
__destruct destruct method, php will call this method before the object is destroyed (i.e. before it is cleared from memory). By default, php only releases the memory occupied by object properties and destroys object-related resources. The destructor allows you to execute arbitrary code after using an object to clear memory. When php decides that your script is no longer related to the object, the destructor will be called.
In a function's namespace, this happens when the function returns. 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 pointing to the object. Usually assign the variable to null or call unset.
Object assignment in php5 is a reference assignment used. If you want to copy an object, you need to use the clone method. When calling this method, the object will automatically call the __clone magic method. If some initialization operations are required during object copying, it can be implemented in the __clone method.
__tostring methodIt is called automatically when converting an object into a string, such as when printing an object using echo.
Prior to php5.2.0, the __tostring method can only take effect when using echo() or print() in combination. After php5.2.0, it can take effect in any string environment (for example through printf(), using the %s modifier), but cannot be used in non-string environments (for example using the %d modifier). From php5.2.0, if an object with an undefined __tostring method is converted into a string, an e_recoverable_error error will be reported.
__wakeupCalled during reverse serialization
serialize() checks whether there is a function with the magic name __sleep in the class. If so, the function will run before any serialization. It can clear the object and should return an array containing all the variable names in the object that should be serialized.
The purpose of using __sleep is to close any database connections the object may have, submit waiting data, or perform similar cleanup tasks. In addition, this function is also useful if there are very large objects that do not need to be stored completely.
PHP Conversely, unserialize() checks for the existence of a function with the magic name __wakeup. If present, this function can rebuild any resources the object may have. The purpose of using __wakeup is to rebuild any database connections that may be lost in serialization and to handle other reinitialization tasks.
When trying to call an object in the form of calling a function, the __invoke method is automatically called.
ItsWorks like the magic method of __callstatic(), which is to handle static method calls.
php does strengthen the definition of the __callstatic() method; it must be public and must be declared static. Likewise, the __call() magic method must be defined as public, and all other magic methods must be so.