__invoke(),在PHP对象初始化后,直接像变量函数一样调用时,自动调用此方法。
__callStatic(),在调用不存在的静态方法时自动调用。必须是公开的且必须是静态的。
__invoke()
class Person {
public $name;
public $age;
public $sex;
public function __construct( $name, $age, $sex ) {
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
function say() {
echo "我叫{$this->name},今年{$this->age}岁
";
}
function __invoke( $a, $b, $c ) {
echo '在对象引用时,后面加上()调用这个方法
';
echo "{$a}---{$b}---{$c}";
}
}
$o = new Person( 'xmoon', 18, '男' );
$o(1,2,3);
__callStatic()
/*
猫斯基 www.maosiji.com
*/
class Person {
public $name;
public $age;
public $sex;
public function __construct( $name, $age, $sex ) {
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
function say() {
echo "我叫{$this->name},今年{$this->age}岁
";
}
public static function __callstatic( $method, $args ) {
echo "你调用的静态方法 $method(".implode(",", $args)."),不存在。
";
}
}
$o = new Person( 'xmoon', 18, '男' );
$o::hello();
$o::hello(1);
$o::hello(1,2);
/*
打印结果:
你调用的静态方法 hello(),不存在。
你调用的静态方法 hello(1),不存在。
你调用的静态方法 hello(1,2),不存在。
*/