我見過一些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...
}
它是很好的做法,有一個單獨的類都一切或者我應該寫代碼,更加模塊化?
如果您能解釋爲什麼在您可能提供的任何答案中,我將不勝感激。
編寫更多模塊化代碼。原因太多,無法在這裏回答清楚的答案。去做就對了。 – 2015-02-23 22:34:50
你的問題很糟糕,閱讀OOP,因爲我們可以看到,你不明白它。您的示例代碼在這種情況下完全沒有意義。 – 2015-02-23 22:35:05
@kmlnvm我的例子不是我會怎麼做。正如我在我的問題中提到的,我一直看到巨大的整體類定義,這讓我質疑我自己的模塊化方法。因此,我爲什麼問這個問題。 – henrywright 2015-02-23 22:39:32