發表文章

目前顯示的是 9月, 2020的文章

PHP Traits

What is Traits? PHP 僅支援單一繼承: 子類別只能繼承單一物件。 若是一個類需要繼承多種行為怎麼辦呢? Traits 解決了這個問題。 Traits 就是解決在單線繼承的限制下,讓程式碼能夠重複使用。並降低複雜度。 Traits 用於聲明可以在多個 class 中使用的屬性(property)/函式(function),可以是抽象或是任何可視性(public、protect 、private),甚至是靜態(abstract)屬性。 如何使用 建立語法 trait TraitName { // some code... } 使用語法 class newClass { use TraitName; } Example trait message { function msg() { echo 'Welcome to my home.'; } } class Welcome { use message; } $welcome = new Welcome(); $welcome->msg(); 可以同時使用多個 trait messageFriendly { function msg() { echo 'Welcome to my home.'; } } trait messageQuestion { function msgQuestion() { echo 'Why are you here?'; } } class Welcome { use messageFriendly, messageQuestion; } $welcome = new Welcome(); $welcome->msg(); echo '<br/>'; $welcome->msgQuestion(); 若是名稱重複了呢? 函式名稱重複是會造成錯誤的。 Fatal error: Trait method msg has not been applied, because there are collisions with oth...