PHP __invoke()的用法(附带实例)
在 PHP 程序中,以调用函数的方式调用一个对象时,__invoke() 方法会被自动执行。
下面的代码在 tAuto 类中添加 __invoke() 方法。
下面的代码在 tAuto 类中添加 __invoke() 方法。
<?php
class tAuto
{
//
public function __invoke($model, $doors)
{
$this->model = $model;
$this->doors = $doors;
}
// 构造函数
public function __construct($doors=4)
{
$this->setDoors($doors);
}
// 析构函数
//public function __destruct()
//{
// echo "车辆报废了...";
//}
//
public $model;
private $doors;
// 其他代码
}
?>
下面的代码测试 __invoke() 方法的效果。
$car("A33",4);
代码执行结果为:
A33
4
ICP备案:
公安联网备案: