Home > Backend Development > PHP Tutorial > Specific application explanation of PHP5 magic function_PHP tutorial

Specific application explanation of PHP5 magic function_PHP tutorial

WBOY
Release: 2016-07-15 13:29:17
Original
881 people have browsed it

In PHP5 magic function 1, __construct() When an object is instantiated, this method of the object is first called.

<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>
Copy after login

The output is:

start

We know the php5 object model and The function with the same class name is the constructor of the class, so if we define the constructor and __construct() method at the same time, php5 will call the constructor by default and not the __construct() function, so __construct() is used as the class's constructor. Default constructor

PHP5 magic function 2, __destruct() This method is called when an object is deleted or the object operation terminates.

<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>
Copy after login

will output

end

We can perform operations such as releasing resources at the end of the object operation

PHP5 magic function 3, __get() When trying to read an attribute that does not exist time is called.

If you try to read a property that does not exist in an object, PHP will give an error message. If you add the __get method to the class, and we can use this function to implement various operations similar to reflection in 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>
Copy after login

PHP5 magic function 4, __set() is called when trying to write a value to a property that does not exist.

<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>
Copy after login

PHP5 magic function 5, __call() When trying to call a method that does not exist on an object, call this method.

<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>
Copy after login

The program will output:

The getName method you want to call does not exist. The parameters are: Array

(

[0] => aning

[1] => go

)

PHP5 magic function 6,__toString() when Called when printing an object

This method is similar to java's toString method. When we print the object directly, this function is called back

<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>
Copy after login

run echo $t; When, $t->__toString(); will be called to output

Print Test

PHP5 Magic Function 7, __clone() When the object is cloned, it will be called

<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>
Copy after login

PHP5 Magic Function 8. By the way, let me introduce several very COOl experimental functions provided in php5

(1) runkit_method_rename

This function can dynamically change the name of the function we call.

<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>
Copy after login

(2) runkit_method_add

This PHP5 magic function can dynamically add functions to a class

<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>
Copy after login

(3)runkit_method_copy

You can copy the functions in class A to the class B and rename the PHP5 magic function

<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>
Copy after login

(4) runkit_method_redefine

Dynamicly modify the return value of the function

This PHP5 magic function allows us to easily implement MOCK testing of classes! Isn’t it cool?

  1. class Example {  
  2.     function foo() {  
  3.         return "foo! ";  
  4.     }  
  5. }  
  6. //创建一个测试对象  
  7. $e = new Example();  
  8. // 在测试对象之前输出  
  9. echo "Before: " . $e->foo();  
  10. // 修改返回值  
  11. runkit_method_redefine(  
  12.     'Example',  
  13.     'foo',  
  14.     '',  
  15.     'return "bar! ";',  
  16.     RUNKIT_ACC_PUBLIC  
  17. );  
  18. // 执行输出  
  19. echo "After: " . $e->foo();  

(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>
Copy after login

以上所介绍的代码就是关于PHP5魔术函数的具体应用方式。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446384.htmlTechArticle在 PHP5魔术函数1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。 classTest { function__construct() { echo"before"; } } $ t =...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template