首页 > php教程 > php手册 > 正文

PHP魔术方法的使用示例,php魔术示例

php中文网
发布: 2016-06-13 09:00:01
原创
916人浏览过

php魔术方法的使用示例,php魔术示例

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

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

index.php
复制代码 代码如下:
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\Common\Loader::autoload');

$obj = new CommonObject(); 

//在php中访问一个不存在的对象属性时
echo $obj->title;

会抛出一个错误:Notice: Undefined property: CommonObject::$title in D:practisephpdesignpsr0index.php on line 9

当在Common/Object.php 中添加 __set 和 __get 方法后

立即学习PHP免费学习笔记(深入)”;

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 CommonObject(); 

$obj->title = 'hello';
echo $obj->title;

执行 index.php,页面输出:
复制代码 代码如下:
string 'CommonObject::__set' (length=20)
string 'CommonObject::__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 CommonObject(); 

//当执行一个不存在的php方法时
$obj->test('hello',123);

执行 index.php 会报一个致命错误:Fatal error: Call to undefined method CommonObject::test() in D:practisephpdesignpsr0index.php on line 9

如果在 Common/Object 中定义一个__call 方法,则会在方法不存在时自动回调:
复制代码 代码如下:
namespace Common;

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

index.php

复制代码 代码如下:
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\Common\Loader::autoload');

$obj = new CommonObject(); 

//当执行一个不存在的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 "; //返回一个字符串作为返回值       
    }
}

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

index.php
复制代码 代码如下:
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\Common\Loader::autoload');

//执行一个不存在的静态方法
echo CommonObject::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 CommonObject();

echo $obj;

此时会报错: Catchable fatal error: Object of class CommonObject could not be converted to string in D:practisephpdesignpsr0index.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 CommonObject();

echo $obj("test");

Object.php
复制代码 代码如下:
namespace Common;

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

页面输出:
复制代码 代码如下:
string 'test' (length=4)
invoke

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号