/**
1. __get/__set
2. __call/__callStatic
3. __toString
4. __invoke
*/
<?php
namespace tests\test;
class A
{
protected $array = array();
function __set($key, $value)
{
$this->array[$key] = $value;
}
function __get($key)
{
return $this->array[$key];
}
function __call($func, $params)
{
var_dump($func, $params);
echo "<br>";
}
public static function __callStatic($func, $params)
{
var_dump($func, $params);
echo "<br>";
}
static function getArr()
{
print_r(__FILE__);
echo "<br>";
}
function __toString()
{
return __CLASS__;
}
function __invoke($param)
{
var_dump($param);
return "invoke";
}
}
<?php
spl_autoload_register('autoload');
function autoload($class)
{
require __DIR__.'/'.$class.'.php';
}
$a = new tests\test\A();
$a->title = 1; // 对象属性不存在,自动回调__set
print_r($a->title);// 对象属性不存在,自动回调__get
echo "<br>";
tests\test\A::getArr();
$a->getTitle(12, 34); //方法不存在,自动回调__call
tests\test\A::getTitle(); //静态(类)方法不存在,自动回调__callStatic
echo $a; //自动回调__toString
echo "<br>";
echo $a(1); //自动回调__invoke