在PHP中如何使用魔术方法

发布时间:2020-12-23 15:05:44 作者:Leah
来源:亿速云 阅读:116

在PHP中如何使用魔术方法?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

① __get/__set:将对象的属性进行接管

当访问一个不存在的对象属性时:

index.php

复制代码 代码如下:


<?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

复制代码 代码如下:


<?php
namespace Common;

class Object{
    function __set($key,$value){
    }
   
    function __get($key){
    }
}

再执行 index.php,不会再报错。

再次修改 Common/Object.php

复制代码 代码如下:


<?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

复制代码 代码如下:


<?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:

复制代码 代码如下:


<?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 方法,则会在方法不存在时自动回调:

复制代码 代码如下:


<?php
namespace Common;

class Object{   
    function __call($func, $param){ //$func 方法名 $param 参数
        var_dump($func, $param);
        return "magic function\n"; //返回一个字符串作为返回值
    }
}

index.php

复制代码 代码如下:


<?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

复制代码 代码如下:


<?php
namespace Common;

class Object{
    static function __callStatic($name, $arguments) {
        var_dump($name, $arguments);
        return "magic function\n"; //返回一个字符串作为返回值       
    }
}

注意:__callStatic 方法也要声明成静态方法

index.php

复制代码 代码如下:


<?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

复制代码 代码如下:


<?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 方法

复制代码 代码如下:


<?php
namespace Common;

class Object{
    function __toString() {
        return __CLASS__;
    }
}

④ __invoke:将一个 PHP 对象当成一个函数来执行时,会回调此魔术方法

index.php

复制代码 代码如下:


<?php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

echo $obj("test");

Object.php

复制代码 代码如下:


<?php
namespace Common;

class Object{
    function __invoke($param) {
        var_dump($param);
        return 'invoke';
    }
}

页面输出:

复制代码 代码如下:


string 'test' (length=4)
invoke

关于在PHP中如何使用魔术方法问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. php魔术方法
  2. PHP中魔术方法如何使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:魔术方法如何在PHP项目中使用

下一篇:使用php读取csv文件出现乱码如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》