这篇文章主要介绍了PHP魔术方法的使用示例,本文分别讲解了__get、__set、__call、__callStatic、__toString、 __invoke等魔术方法的使用,需要的朋友可以参考下
① __get/__set:将对象的属性进行接管
当访问一个不存在的对象属性时:
index.php
代码如下:
<?php
define(‘BASEDIR’,__DIR__); //定义根目录常量
include BASEDIR.’/Common/Loader.php’;
spl_autoload_register(‘\\Common\\Loader::au来源gaodai#ma#com搞*代#码网toload’);
define(‘BASEDIR’,__DIR__); //定义根目录常量
include BASEDIR.’/Common/Loader.php’;
spl_autoload_register(‘\\Common\\Loader::au来源gaodai#ma#com搞*代#码网toload’);
$obj = new \Common\Object();
//在php中访问一个不存在的对象属性时
echo $obj->title;
以上就是PHP魔术方法的使用示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!