Object类
<?php
/**
* Created by PhpStorm.
* User: dragon
* Date: 2017/5/4
* Time: 14:38
*/
namespace core\lib;
class Object
{
protected $array = [];
public function __set($name, $value)
{
// TODO: Implement __set() method.
var_dump(__METHOD__);
$this->array[$name] = $value;
}
public function __get($name)
{
// TODO: Implement __get() method.
var_dump(__METHOD__);
return $this->array[$name];
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
var_dump($name,$arguments);
return "magic function\n";
}
static function __callStatic($name, $arguments)
{
// TODO: Implement __callStatic() method.
var_dump($name,$arguments);
return "magic static function\n";
}
public function __toString()
{
// TODO: Implement __toString() method.
return __CLASS__;
}
public function __invoke($param)
{
// TODO: Implement __invoke() method.
var_dump($param);
return "invoke";
}
}
Object类中包含_set()和_get()方法后,就可以访问到未定义的属性
include CORE.'/z_frame.php';
spl_autoload_register('\core\z_frame::load');
$obj = new \core\lib\Object();
$obj->title = 'zdc';
echo $obj->title;
Object类中包含__call()方法后,可以调用未定义的方法
include CORE.'/z_frame.php';
spl_autoload_register('\core\z_frame::load');
$obj = new \core\lib\Object();
echo $obj->test("zdc",123);
Object类中包含__callStatic()方法后,可以调用未定义的静态方法
include CORE.'/z_frame.php';
spl_autoload_register('\core\z_frame::load');
$obj = new \core\lib\Object();
echo \core\lib\Object::test("zdc",123);
用__toString()方法可将一个类对象当字符串返回
include CORE.'/z_frame.php';
spl_autoload_register('\core\z_frame::load');
$obj = new \core\lib\Object();
echo $obj;
在Object类中定义__invoke()函数可以将对象当函数使用
include CORE.'/z_frame.php';
spl_autoload_register('\core\z_frame::load');
$obj = new \core\lib\Object();
echo $obj("zdc");