您好,欢迎访问一九零五行业门户网

PHP魔术方法的使用示例

这篇文章主要介绍了php魔术方法的使用示例,本文分别讲解了__get、__set、__call、__callstatic、__tostring、 __invoke等魔术方法的使用,需要的朋友可以参考下
① __get/__set:将对象的属性进行接管
当访问一个不存在的对象属性时:
index.php
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
$obj = new \common\object(); 
//在php中访问一个不存在的对象属性时
echo $obj->title;
会抛出一个错误:notice: undefined property: common\object::$title in d:\practise\php\design\psr0\index.php on line 9
当在common/object.php 中添加 __set 和 __get 方法后
object.php
复制代码 代码如下:
namespace common;
class object{
    function __set($key,$value){
    }
function __get($key){
    }
}
再执行 index.php,,不会再报错。
再次修改 common/object.php
复制代码 代码如下:
namespace common;
class object{
    protected $array = array();
function __set($key,$value){
        var_dump(__method__);
        $this->array[$key] = $value;
    }
function __get($key){
        var_dump(__method__);
        return $this->array[$key];
    }
}
index.php
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
$obj = new \common\object(); 
$obj->title = 'hello';
echo $obj->title;
执行 index.php,页面输出:
复制代码 代码如下:
string 'common\object::__set' (length=20)
string 'common\object::__get' (length=20)
hello
② __call/__callstatic:控制 php 对象方法的调用(__callstatic 用来控制类的静态方法)
当执行一个不存在的php方法时
index.php:
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
$obj = new \common\object(); 
//当执行一个不存在的php方法时
$obj->test('hello',123);
执行 index.php 会报一个致命错误:fatal error: call to undefined method common\object::test() in d:\practise\php\design\psr0\index.php on line 9
如果在 common/object 中定义一个__call 方法,则会在方法不存在时自动回调:
复制代码 代码如下:
namespace common;
class object{   
    function __call($func, $param){ //$func 方法名 $param 参数
        var_dump($func, $param);
        return magic function\n; //返回一个字符串作为返回值
    }
}
index.php
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
$obj = new \common\object(); 
//当执行一个不存在的php方法时
echo $obj->test('hello',123);
页面输出:
复制代码 代码如下:
string 'test' (length=4)
array
  0 => string 'hello' (length=5)
  1 => int 123
magic function
当调用一个不存在的静态方法时
common/object.php
复制代码 代码如下:
namespace common;
class object{
    static function __callstatic($name, $arguments) {
        var_dump($name, $arguments);
        return magic function\n; //返回一个字符串作为返回值       
    }
}
注意:__callstatic 方法也要声明成静态方法
index.php
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
//执行一个不存在的静态方法
echo common\object::test(hello,1234);
执行 index.php ,页面输出:
复制代码 代码如下:
string 'test' (length=4)
array
  0 => string 'hello' (length=5)
  1 => int 1234
magic function
③ __tostring:将一个 php 对象转换成一个字符串
index.php
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
$obj = new \common\object();
echo $obj;
此时会报错: catchable fatal error: object of class common\object could not be converted to string in d:\practise\php\design\psr0\index.php on line 8
在 object.php 中添加 __tostring 方法
复制代码 代码如下:
namespace common;
class object{
    function __tostring() {
        return __class__;
    }
}
④ __invoke:将一个 php 对象当成一个函数来执行时,会回调此魔术方法
index.php
复制代码 代码如下:
define('basedir',__dir__); //定义根目录常量
include basedir.'/common/loader.php';
spl_autoload_register('\\common\\loader::autoload');
$obj = new \common\object();
echo $obj(test);
object.php
复制代码 代码如下:
namespace common;
其它类似信息

推荐信息