2015-02-23 76 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