2015-02-23 74 views
-5

我見過一些PHP類定義,其中包含許多與實際類無關(或至多鬆散相關)的方法。例如:定義PHP類時的良好做法

class Dog { 
    public $var; 

    public function __construct() {} 

    public function eat() { 
     // Code here is relevant to dog. 
    } 

    public function sleep() { 
     // Code here is relevant to dog. 
    } 

    public function play() { 
     // Code here is relevant to dog. 
    } 

    public function pay_milkman() { 
     // The code here isn't really related to the life of a dog but is useful in the rest of the project. 
    } 

    public function go_to_work() { 
     // The code here isn't really related to the life of a dog but is useful in the rest of the project. 
    } 

    etc... 
} 

它是很好的做法,有一個單獨的類都一切或者我應該寫代碼,更加模塊化?

如果您能解釋爲什麼在您可能提供的任何答案中,我將不勝感激。

+0

編寫更多模塊化代碼。原因太多,無法在這裏回答清楚的答案。去做就對了。 – 2015-02-23 22:34:50

+0

你的問題很糟糕,閱讀OOP,因爲我們可以看到,你不明白它。您的示例代碼在這種情況下完全沒有意義。 – 2015-02-23 22:35:05

+0

@kmlnvm我的例子不是我會怎麼做。正如我在我的問題中提到的,我一直看到巨大的整體類定義,這讓我質疑我自己的模塊化方法。因此,我爲什麼問這個問題。 – henrywright 2015-02-23 22:39:32

回答

1

我覺得你的類只需要一些特殊情況:

class Dog { 
    public $var; 

    public function __construct() {} 

    public function eat() { 
     // Code here is relevant to dog. 
    } 

    public function sleep() { 
     // Code here is relevant to dog. 
    } 

    public function play() { 
     // Code here is relevant to dog. 
    } 
} 

class ExtremelySmartAndWellTrainedDog extends Dog { 
    public function pay_milkman() { 
     // Code here is relevant to a well-trained dog 
    } 
} 

class SheepDog extends Dog { 
    public function go_to_work() { 
     // Code here is what sheepdogs do 
    } 
} 

當然,如果有可能有一隻狗,既聰明/訓練有素工作,那麼我會執行這些方法而不是特質。

+0

感謝您的例子。我認爲在ExtremelySmartAndWellTrainedDog和SheepDog的情況下擴展類是有意義的。你是對的,因爲代碼與什麼樣的羊狗(和訓練有素的狗)有關。但也許我可以舉一個真實世界的例子。在WordPress插件目錄中,有許多插件都包含相同類定義中的'load_textdomain()'和'enqueue_styles()'方法。這些方法與班級沒有直接關係,並且(在我看來)會更好地編碼爲獨立功能。 – henrywright 2015-02-23 23:37:10

+0

我認爲這歸結於什麼屬於什麼的主觀意見,@henrywright。我只對WP稍微熟悉,但由於插件需要呈現樣式,我可以看到爲什麼'enqueue_styles()'可能與插件類有關。作爲插件風格類會更好嗎?可能,我並不是真的有看法。我總是非常清楚你的觀點 - 無關的事情不應該出現在同一個班上。 – halfer 2015-02-24 00:24:20

+1

謝謝@halfer,我很欣賞你採取討論這些問題的時間,這很有幫助! – henrywright 2015-02-24 00:26:15

1

狗不會支付牛奶工,也不會(通常)工作,所以這些功能不應該在狗類中。這些功能會去像Person類,誰可能擁有一個或更多的狗通過兩個類之間的關係,即:

class Person { 
    public $dogs; 
    public function buy_dog() { 
    $dog = new Dog; 
    $this->dogs[] = $dog; 
    } 
} 
0

從我瞭解你,詢問是否使用類中的許多功能是壞方法。在我看來,這取決於。在開發面向對象的應用程序時,我總是想到我可以用於該類的所有可能的功能/方法。

還有更多。例如,有飛行的鳥類和其他類型的鳥類,因此我的面向對象的方法如下。

class Bird 
{ 
    public $canfly; 

    public function __construct($canfly) 
    { 
     $this->canfly = $canfly; 
    } 

    public function canFly() 
    { 
     return $this->canfly; 
    } 
} 

企鵝是一隻鳥,但不飛。

class Penguin extends Bird 
{ 
    public function canFly() 
    { 
     // return parent::canFly(); <- Original Property from parent. 
     return false; // Override 
    } 
} 

測試類

$class = new Penguin(false); 
if($class->canFly() == false) 
{ 
    print("Bird can't Flye"); 
} 

有那裏許多例子。點擊here獲得一個非常好的教程。

+1

是的,對不起,在全編程Java編程現在將編輯它。 – 2015-02-23 23:08:25

+0

另外,「企鵝」似乎沒有任何重寫。我想你可能會重寫'canFly'並返回'false',也許? – halfer 2015-02-23 23:09:53

+0

這個例子只是爲了演示繼承一個同類型的類如何繼承它的父類屬性等。我會再次編輯我的問題,使其更容易理解。 – 2015-02-23 23:16:19