定义 PHP class 时的良好做法
Good practice when defining a PHP class
我见过一些 PHP class 定义,其中包含许多看起来与实际 class 无关(或充其量是松散相关)的方法。例如:
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...
}
让一个人 class 做 所有事情 是好习惯还是我应该编写更模块化的代码?
如果您能在您提供的任何答案中解释原因,我将不胜感激。
Dogs 不支付送奶工的工资,他们(通常)也不工作,所以这些功能不应该在 Dog class 中。这些功能将进入 class 中,例如 Person
,他们可能通过两个 class 之间的关系拥有一只或多只狗,即:
class Person {
public $dogs;
public function buy_dog() {
$dog = new Dog;
$this->dogs[] = $dog;
}
}
据我了解,您询问在 class 中使用大量函数是否是一种糟糕的方法。在我看来,这真的取决于。在开发面向对象的应用程序时,我总是会想到我可以为此使用的所有可能的功能/方法 class。
还有更多。例如,有些鸟会飞,有些则不会,所以我的面向对象方法如下。
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
}
}
测试 classes
$class = new Penguin(false);
if($class->canFly() == false)
{
print("Bird can't Flye");
}
有很多这样的例子。单击 here 获取非常好的教程。
我认为您的 class 只需要一些特殊情况:
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
}
}
当然,如果有一只狗可以同时 smart/well-trained 和 工作,那么我会在 traits 中实现这些方法。
我见过一些 PHP class 定义,其中包含许多看起来与实际 class 无关(或充其量是松散相关)的方法。例如:
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...
}
让一个人 class 做 所有事情 是好习惯还是我应该编写更模块化的代码?
如果您能在您提供的任何答案中解释原因,我将不胜感激。
Dogs 不支付送奶工的工资,他们(通常)也不工作,所以这些功能不应该在 Dog class 中。这些功能将进入 class 中,例如 Person
,他们可能通过两个 class 之间的关系拥有一只或多只狗,即:
class Person {
public $dogs;
public function buy_dog() {
$dog = new Dog;
$this->dogs[] = $dog;
}
}
据我了解,您询问在 class 中使用大量函数是否是一种糟糕的方法。在我看来,这真的取决于。在开发面向对象的应用程序时,我总是会想到我可以为此使用的所有可能的功能/方法 class。
还有更多。例如,有些鸟会飞,有些则不会,所以我的面向对象方法如下。
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
}
}
测试 classes
$class = new Penguin(false);
if($class->canFly() == false)
{
print("Bird can't Flye");
}
有很多这样的例子。单击 here 获取非常好的教程。
我认为您的 class 只需要一些特殊情况:
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
}
}
当然,如果有一只狗可以同时 smart/well-trained 和 工作,那么我会在 traits 中实现这些方法。