偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

PHP5魔術(shù)函數(shù)的具體應(yīng)用講解

開(kāi)發(fā) 后端
PHP5魔術(shù)函數(shù)扥個(gè)應(yīng)用可以幫助我們簡(jiǎn)化大量的編程代碼,讓系統(tǒng)變得更加輕便。我們?yōu)榇伺e出了PHP5魔術(shù)函數(shù)的一些應(yīng)用方式,希望對(duì)大家有所幫助。

PHP5發(fā)布之后,我們就能夠從中了解多很多新的知識(shí)點(diǎn),比如面向?qū)ο蟮囊恍┬碌奶匦缘?。今天,我們就為大家介紹PHP5中的一個(gè)新的方法——PHP5魔術(shù)函數(shù),它能夠幫助我們簡(jiǎn)化編碼,更好的設(shè)計(jì)我們的系統(tǒng)。

#t#PHP5魔術(shù)函數(shù)1,__construct() 當(dāng)實(shí)例化一個(gè)對(duì)象的時(shí)候,這個(gè)對(duì)象的這個(gè)方法首先被調(diào)用。

  1. class Test   
  2. {   
  3.  function __construct()   
  4.  {   
  5.   echo "before";   
  6.  }   
  7. }   
  8. $t = new Test();  

輸出是:

start

我們知道php5對(duì)象模型 和類名相同的函數(shù)是類的構(gòu)造函數(shù),那么如果我們同時(shí)定義構(gòu)造函數(shù)和__construct()方法的話,php5會(huì)默認(rèn)調(diào)用構(gòu)造函數(shù)而不會(huì)調(diào)用__construct()函數(shù),所以__construct()作為類的默認(rèn)的構(gòu)造函數(shù)

PHP5魔術(shù)函數(shù)2,__destruct() 當(dāng)刪除一個(gè)對(duì)象或?qū)ο蟛僮鹘K止的時(shí)候,調(diào)用該方法。

  1. class Test   
  2. {   
  3.  function __destruct()   
  4.  {   
  5.   echo "end";   
  6.  }   
  7. }   
  8. $t = new Test(); 

將會(huì)輸出

end

我們就可以在對(duì)象操作結(jié)束的時(shí)候進(jìn)行釋放資源之類的操作

PHP5魔術(shù)函數(shù)3,__get() 當(dāng)試圖讀取一個(gè)并不存在的屬性的時(shí)候被調(diào)用。

如果試圖讀取一個(gè)對(duì)象并不存在的屬性的時(shí)候,PHP就會(huì)給出錯(cuò)誤信息。如果在類里添加__get方法,并且我們可以用這個(gè)函數(shù)實(shí)現(xiàn)類似java中反射的各種操作

  1. class Test   
  2. {   
  3.  public function __get($key)   
  4.  {   
  5.   echo $key . " 不存在";   
  6.  }   
  7. }   
  8.  
  9.  
  10. $t = new Test();   
  11. echo $t->name;   
  12.  
  13. 就會(huì)輸出:  
  14. name 不存在  

PHP5魔術(shù)函數(shù)4,__set() 當(dāng)試圖向一個(gè)并不存在的屬性寫入值的時(shí)候被調(diào)用。

  1. class Test   
  2. {   
  3.  public function __set($key,$value)   
  4.  {   
  5.   echo '對(duì)'.$key . "附值".$value;   
  6.  }   
  7. }   
  8.  
  9.  
  10. $t = new Test();   
  11. $t->name = "aninggo";   
  12.  
  13. 就會(huì)輸出:  
  14. 對(duì) name 附值 aninggo 

PHP5魔術(shù)函數(shù)5,__call() 當(dāng)試圖調(diào)用一個(gè)對(duì)象并不存在的方法時(shí),調(diào)用該方法。

  1. class Test   
  2. {   
  3.  public function __call($Key, $Args)   
  4.  {   
  5.   echo "您要調(diào)用的 {$Key} 方法不存在。你傳入的參數(shù)是:" . print_r($Args, true);   
  6.  }   
  7. }   
  8.  
  9. $t = new Test();   
  10. $t->getName(aning,go);  

程序?qū)?huì)輸出:

您要調(diào)用的 getName 方法不存在。參數(shù)是:Array

(

[0] => aning

[1] => go

)

PHP5魔術(shù)函數(shù)6,__toString() 當(dāng)打印一個(gè)對(duì)象的時(shí)候被調(diào)用

這個(gè)方法類似于java的toString方法,當(dāng)我們直接打印對(duì)象的時(shí)候回調(diào)用這個(gè)函數(shù)

  1. class Test   
  2. {   
  3.  public function __toString()   
  4.  {   
  5.   return "打印 Test";   
  6.  }   
  7. }   
  8.  
  9.  
  10. $t = new Test();   
  11.  
  12. echo $t;   

運(yùn)行echo $t;的時(shí)候,就會(huì)調(diào)用$t->__toString();從而輸出

打印 Test

PHP5魔術(shù)函數(shù)7,__clone() 當(dāng)對(duì)象被克隆時(shí),被調(diào)用

  1. class Test   
  2. {   
  3.  
  4.  public function __clone()   
  5.  {   
  6.   echo "我被復(fù)制了!";   
  7.  }   
  8. }  
  9.  
  10. $t = new Test();   
  11. $t1 = clone $t;  
  12.  
  13. 程序輸出:  
  14. 我被克隆了! 

PHP5魔術(shù)函數(shù)8.順便介紹下php5中提供的幾個(gè)非常COOl的實(shí)驗(yàn)性函數(shù)

(1)runkit_method_rename

這個(gè)函數(shù)可以動(dòng)態(tài)的改變我們所調(diào)用的函數(shù)的名字。

  1. class Test   
  2. {   
  3.  
  4.  function foo() {  
  5.         return "foo! ";  
  6.     }  
  7.  
  8. }   
  9.  
  10. runkit_method_rename(  
  11.     'Test', //類名  
  12.     'foo',//實(shí)際調(diào)用的函數(shù)  
  13.     'bar'//顯示調(diào)用的函數(shù)  
  14. );  
  15.  
  16. echo Test::bar();  
  17.  
  18. 程序?qū)⑤敵? 
  19.    
  20. foo!  

(2) runkit_method_add

這個(gè)PHP5魔術(shù)函數(shù)可以動(dòng)態(tài)的向類中添加函數(shù)

  1. class Test   
  2. {   
  3.  
  4.  function foo() {  
  5.         return "foo! ";  
  6.     }  
  7.  
  8. }   
  9.  
  10. runkit_method_add(  
  11.     Test,  //類名  
  12.     'add', //新函數(shù)名  
  13.     '$num1, $num2',//傳入?yún)?shù)  
  14.     'return $num1 + $num2;',//執(zhí)行的代碼  
  15.     RUNKIT_ACC_PUBLIC  
  16. );  
  17.  
  18. // 調(diào)用   
  19. echo $e->add(12, 4); 

(3)runkit_method_copy

可以把A類中的函數(shù)拷貝到類B中并對(duì)PHP5魔術(shù)函數(shù)重命名

  1. class Foo {  
  2.     function example() {  
  3.         return "foo! ";  
  4.     }  
  5. }  
  6.  
  7. class Bar {  
  8.     //空類  
  9. }  
  10.  
  11. //執(zhí)行拷貝  
  12. runkit_method_copy('Bar', 'baz', 'Foo', 'example');  
  13.  
  14. //執(zhí)行拷貝后的函數(shù)  
  15. echo Bar::baz();  

(4) runkit_method_redefine

動(dòng)態(tài)的修改函數(shù)的返回值

這個(gè)PHP5魔術(shù)函數(shù)可以讓我們輕松的實(shí)現(xiàn)對(duì)類的MOCK測(cè)試!是不是很COOL呢

  1. class Example {  
  2.     function foo() {  
  3.         return "foo! ";  
  4.     }  
  5. }  
  6. //創(chuàng)建一個(gè)測(cè)試對(duì)象  
  7. $e = new Example();  
  8. // 在測(cè)試對(duì)象之前輸出  
  9. echo "Before: " . $e->foo();  
  10. // 修改返回值  
  11. runkit_method_redefine(  
  12.     'Example',  
  13.     'foo',  
  14.     '',  
  15.     'return "bar! ";',  
  16.     RUNKIT_ACC_PUBLIC  
  17. );  
  18. // 執(zhí)行輸出  
  19. echo "After: " . $e->foo();  

(5)runkit_method_remove

這個(gè)PHP5魔術(shù)函數(shù)就很簡(jiǎn)單了,看名字就能看出來(lái)了,動(dòng)態(tài)的從類中移除函數(shù)

  1. class Test {  
  2.     function foo() {  
  3.         return "foo! ";  
  4.     }  
  5.       
  6.     function bar() {  
  7.         return "bar! ";  
  8.     }  
  9. }  
  10.  
  11. // 移除foo函數(shù)  
  12. runkit_method_remove(  
  13.     'Test',  
  14.     'foo'  
  15. );  
  16.  
  17. echo implode(' ', get_class_methods('Test'));  
  18.  
  19. 程序輸出  
  20. bar  

以上所介紹的代碼就是關(guān)于PHP5魔術(shù)函數(shù)的具體應(yīng)用方式。

責(zé)任編輯:曹凱 來(lái)源: CSDN
相關(guān)推薦

2009-11-24 16:18:14

PHP5析構(gòu)函數(shù)

2009-11-18 18:33:23

Linux PHP5安

2010-11-03 10:31:51

PHP魔術(shù)方法

2009-12-11 17:33:56

PHP5常用函數(shù)

2009-11-23 19:33:12

PHP5多態(tài)性

2009-11-23 20:00:25

PHP5接口PHP5抽象類

2009-11-23 16:43:03

PHP5安裝GD庫(kù)

2009-12-03 17:18:15

PHP strtoti

2009-11-30 16:08:32

PHP addslas

2009-03-16 16:08:09

PHP異常

2009-11-23 17:56:44

PHP緩存機(jī)制

2009-11-24 17:01:39

PHP5多重繼承

2009-12-03 09:49:59

PHP分頁(yè)導(dǎo)航函數(shù)

2011-03-11 14:02:55

LAMP安裝PHP5

2019-03-08 08:55:16

PHP7PHP5web安全

2009-11-23 13:27:22

PHP5指針

2010-06-23 15:41:44

Linux Bash

2009-11-23 14:17:50

PHP 5.3閉包語(yǔ)法

2009-11-17 14:01:01

Apache 2 PH

2009-11-18 14:45:02

PHP5 Sessio
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)