我见过一些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