PHP物件導向編程——特殊實踐DAY 4

來源:互聯網
上載者:User
面向 對象特殊實踐
(只有在PHP裡面才有,其他語言面向 對象沒有)
面向 對象--魔術方法
__construct(), __destruct() 建構函式和解構函式
__tostring()
__invoke()
__call(), __callStatic()
__get(), __set(), __isset(), __unset()
__clone()
__tostring()
對象被當作String使用時,這個方法會被自動調用。
Echo $obj;
__invoke()
對象被當成方法調用時,這個方法會被自動調用

$obj(4);

對象轉換為string的時候自動調用public function __tostring(){return "This is the Class MagicTest. ";}// __invoke會在把對象當作一個方法調用的時候自動調用public function __invoke($x){echo "__invoke called with parameter ".$x."\n";}}$obj = new MagicTest();echo $obj."\n";$obj(5);?>

output:
This is the Class MagicTest.
__invoke called with parameter 5

__call()
對象訪問不存在的方法名稱時,__call()方法會被自動調用
__callStatic()
對象訪問不存在的靜態方法名稱時,__callStatic()方法會被自動調用
這兩個方法在PHP裡面也被成為方法的重載(overloading)
注意區分重寫(overwrite)
通過這兩個方法,同一個方法的名稱的調用可以對應不同的方法實現

靜態方法的重載,注意這個方法需要設定為staticpublic static function __callStatic($name,$arguments){echo "Static Calling " . $name . " with parameters: ".implode(",", $arguments)."\n";}}$obj = new MagicTest();//runTest這兩個方法的名稱本來不能完全相同,但是通過魔術方法,可以調用了$obj->runTest("para1","para2"); //沒有聲明這個runTest方法,因為有__call這個魔術方法,也可以被調用MagicTest::runTest("para1","para2"); //沒有聲明這個runTest方法,因為有__callStatic這個魔術方法,也可以被調用?>

Output:
Calling runTest with parameters: para1,para2
Static Calling runTest with parameters: para1,para2


__get(), __set(), __isset(), __unset()
在給不可訪問屬性賦值時,__set()會被調用
讀取不可訪問屬性的值時,__get()會被調用
當對不可訪問屬性調用isset()或empty()時,__isset()會被調用
當對不可訪問屬性調用unset()時,__unset()會被調用
所謂不可訪問屬性,實際上就是在調用某個屬性是發現這個屬性沒有被定義,這時候不同的操縱會觸發不同的魔術方法
這幾個方法也被稱為屬性重載的魔術方法


靜態方法的重載,注意這個方法需要設定為staticpublic static function __callStatic($name,$arguments){echo "Static Calling " . $name . "with parameters: ".implode(",", $arguments)."\n";}//屬性重載public function __get($name){return "Getting the property ".$name."\n";}public function __set($name, $value){echo "Setting the property ".$name." to value " . $value."\n";}public function __isset($name){echo "__isset invoked\n";return true;}public function __unset($name){echo "unsetting property ".$name."\n";}}$obj = new MagicTest();echo $obj->className."\n"; //className未定義,但是通過魔術方法__get,這個方法好像被定義了一樣$obj->className='MagicClassX'; //通過魔術方法__get將className名稱定義為MagicClassXecho '$obj->name is set?'.isset($obj->className)."\n";echo '$obj->name is empty?'.empty($obj->className)."\n";unset($obj->className);?>

Output:
Getting the property className
Setting the property className to value MagicClassX
__isset invoked
$obj->name is set?1 //如果return為true的話,結果顯示為1;如果return為false的話,結果顯示為空白
__isset invoked
$obj->name is empty? //如果return為true的話,結果顯示為空白;如果return為false的話,結果顯示為1
unsetting property className

面向對象--魔術方法
__clone()
對象編程——特殊實踐DAY 4">

name='TBD'; //屏蔽你不想要他複製過去的資料,屏蔽掉他的資料,設定他的初始值}}$james = new NbaPlayer();$james->name = 'James';echo $james->name."\n";$james2 = clone $james;echo "Before set up: James2's: ".$james2->name."\n";$james2->name='James2';echo "James's: ".$james->name;echo "James's: ".$james2->name;?>

output:
James
Before set up: James2's: TBD
James's: James
James's: James2 //James2的資料不會影響James的資料
-------------------------------------------------------------------------------PHP面向 對象編程筆記就此完畢---------------------------------------------------------------------------------------

以上就介紹了PHP物件導向編程——特殊實踐DAY 4,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.