Heim > Artikel > Backend-Entwicklung > PHP5魔术函数的具体应用讲解_PHP教程
在PHP5魔术函数1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> function __construct() </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo "before"; </span></li> <li class=""><span> } </span></li> <li class="alt"> <span>} </span><span></span><span></span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> </ol>
输出是:
start
我们知道php5对象模型 和类名相同的函数是类的构造函数,那么如果我们同时定义构造函数和__construct()方法的话,php5会默认调用构造函数而不会调用__construct()函数,所以__construct()作为类的默认的构造函数
PHP5魔术函数2,__destruct() 当删除一个对象或对象操作终止的时候,调用该方法。
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> function __destruct() </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo "end"; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> </ol>
将会输出
end
我们就可以在对象操作结束的时候进行释放资源之类的操作
PHP5魔术函数3,__get() 当试图读取一个并不存在的属性的时候被调用。
如果试图读取一个对象并不存在的属性的时候,PHP就会给出错误信息。如果在类里添加__get方法,并且我们可以用这个函数实现类似java中反射的各种操作
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public function __get($key) </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo $key . " 不存在"; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> <li class="alt"> <span>echo $t-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>name; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>就会输出: </span></li> <li class=""><span>name 不存在 </span></li> </ol>
PHP5魔术函数4,__set() 当试图向一个并不存在的属性写入值的时候被调用。
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public function __set($key,$value) </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo '对'.$key . "附值".$value; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> <li class="alt"> <span>$t-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"aninggo"</font></span><span>; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>就会输出: </span></li> <li class=""><span>对 name 附值 aninggo </span></li> </ol>
PHP5魔术函数5,__call() 当试图调用一个对象并不存在的方法时,调用该方法。
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public function __call($Key, $Args) </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true); </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> <li class=""> <span>$t-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getName(aning,go); </span> </li> </ol>
程序将会输出:
您要调用的 getName 方法不存在。参数是:Array
(
[0] => aning
[1] => go
)
PHP5魔术函数6,__toString() 当打印一个对象的时候被调用
这个方法类似于java的toString方法,当我们直接打印对象的时候回调用这个函数
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> public function __toString() </span></li> <li class=""><span> { </span></li> <li class="alt"><span> return "打印 Test"; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> <li class="alt"><span> </span></li> <li class=""><span>echo $t; </span></li> </ol>
运行echo $t;的时候,就会调用$t->__toString();从而输出
打印 Test
PHP5魔术函数7,__clone() 当对象被克隆时,被调用
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> </span></li> <li class=""><span> public function __clone() </span></li> <li class="alt"><span> { </span></li> <li class=""><span> echo "我被复制了!"; </span></li> <li class="alt"><span> } </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Test(); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">t1</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">clone</font></span><span> $t; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>程序输出: </span></li> <li class=""><span>我被克隆了! </span></li> </ol>
PHP5魔术函数8.顺便介绍下php5中提供的几个非常COOl的实验性函数
(1)runkit_method_rename
这个函数可以动态的改变我们所调用的函数的名字。
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> </span></li> <li class=""><span> function foo() { </span></li> <li class="alt"><span> return "foo! "; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>runkit_method_rename( </span></li> <li class="alt"><span> 'Test', //类名 </span></li> <li class=""><span> 'foo',//实际调用的函数 </span></li> <li class="alt"><span> 'bar'//显示调用的函数 </span></li> <li class=""><span>); </span></li> <li class="alt"><span> </span></li> <li class=""><span>echo Test::bar(); </span></li> <li class="alt"><span> </span></li> <li class=""><span>程序将输出 </span></li> <li class="alt"><span> </span></li> <li class=""><span>foo! </span></li> </ol>
(2) runkit_method_add
这个PHP5魔术函数可以动态的向类中添加函数
<ol class="dp-xml"> <li class="alt"><span><span>class Test </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> </span></li> <li class=""><span> function foo() { </span></li> <li class="alt"><span> return "foo! "; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>runkit_method_add( </span></li> <li class="alt"><span> Test, //类名 </span></li> <li class=""><span> 'add', //新函数名 </span></li> <li class="alt"><span> '$num1, $num2',//传入参数 </span></li> <li class=""><span> 'return $num1 + $num2;',//执行的代码 </span></li> <li class="alt"><span> RUNKIT_ACC_PUBLIC </span></li> <li class=""><span>); </span></li> <li class="alt"><span> </span></li> <li class=""><span>// 调用 </span></li> <li class="alt"> <span>echo $e-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>add(12, 4); </span> </li> </ol>
(3)runkit_method_copy
可以把A类中的函数拷贝到类B中并对PHP5魔术函数重命名
<ol class="dp-xml"> <li class="alt"><span><span>class Foo { </span></span></li> <li class=""><span> function example() { </span></li> <li class="alt"><span> return "foo! "; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>class Bar { </span></li> <li class=""><span> //空类 </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//执行拷贝 </span></li> <li class=""><span>runkit_method_copy('Bar', 'baz', 'Foo', 'example'); </span></li> <li class="alt"><span> </span></li> <li class=""><span>//执行拷贝后的函数 </span></li> <li class="alt"><span>echo Bar::baz(); </span></li> </ol>
(4) runkit_method_redefine
动态的修改函数的返回值
这个PHP5魔术函数可以让我们轻松的实现对类的MOCK测试!是不是很COOL呢
(5)runkit_method_remove
这个PHP5魔术函数就很简单了,看名字就能看出来了,动态的从类中移除函数
<ol class="dp-xml"> <li class="alt"><span><span>class Test { </span></span></li> <li class=""><span> function foo() { </span></li> <li class="alt"><span> return "foo! "; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> function bar() { </span></li> <li class="alt"><span> return "bar! "; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>// 移除foo函数 </span></li> <li class=""><span>runkit_method_remove( </span></li> <li class="alt"><span> 'Test', </span></li> <li class=""><span> 'foo' </span></li> <li class="alt"><span>); </span></li> <li class=""><span> </span></li> <li class="alt"><span>echo implode(' ', get_class_methods('Test')); </span></li> <li class=""><span> </span></li> <li class="alt"><span>程序输出 </span></li> <li class=""><span>bar </span></li> </ol>
以上所介绍的代码就是关于PHP5魔术函数的具体应用方式。