Home > Article > Backend Development > Detailed explanation of __invoke() method in PHP
__invoke(), the response method when calling an object by calling a function
Function:
When trying to call a function When calling an object, the __invoke() method is automatically called.
Note:
This feature is only valid in PHP 5.3.0 and above.
Go directly to the code:
<?php class Person { public $sex; public $name; public $age; public function __construct($name="", $age=25, $sex='男') { $this->name = $name; $this->age = $age; $this->sex = $sex; } public function __invoke() { echo '这可是一个对象哦'; } } $person = new Person('小明'); // 初始赋值 $person();
View the running results:
这可是一个对象哦
Of course, if you insist on using the object as a function method, you will get the following results:
Fatal error: Function name must be a string in D:\phpStudy\WWW\test\index.php on line 18
The above is the detailed content of Detailed explanation of __invoke() method in PHP. For more information, please follow other related articles on the PHP Chinese website!