3

我有两节课。两个类都使用相同的属性列表。这个属性列表有 75 行长。我想把它放在一个单独的文件中,然后两个类都可以访问。但我无法使用包含。

如果发生更改,如何使文件更短并使属性列表更灵活?

我不确定我是否表达了我的观点,所以我举个例子:

我有类 foo类 bar

LIST OF FRUIT 属性private $applesprivate $bananasprivate $grapes用于这两个类。此外,这两个类都有一些其他属性,这些属性特定于每个类。

我想做这样的事情:

class foo 
{
 private $variable_one
 private $variable_two

 //DEFINE THE LIST OF FRUIT PROPERTIES HERE

 public function blahbla...
}

和另一个文件

class foo 
{
 private $variable_three
 private $variable_four

 //DEFINE THE LIST OF FRUIT PROPERTIES HERE

 public function gibberish...
}

现在因为将来我可能会扩展我的水果列表并添加菠萝和芒果,但去掉香蕉,将列表作为一个文件放在一个单独的地方会很方便,我可以在其中修改它,以及所做的任何更改将被任何使用水果属性列表的类采用。

它还可以帮助我减少文件的长度......就像我说的,我的水果列表目前有 75 行长,在两个类的前面都有这么长的简介相当烦人。

我感谢任何有关如何实现这两个目标(灵活性和短文件)的意见或建议。

非常感谢!

4

3 回答 3

5

听起来这两个类都应该继承自定义公共属性的基类。查看有关对象继承的 PHP 手册

// The base class defines common properties
class FooBase {
  // protected properties will be available to extending classes
  protected $apples;
  protected $bananas;
  protected $oranges;
}
// Foo extends FooBase, inheriting its protected & public properties
class Foo extends FooBase {
  private $variable_one;
  private $variable_two;

  public function __construct() {
    // Initialize some stuff
    $this->apples = 3;
  }
  public function getApples() {
    // $this->apples inherited from FooBase
    echo $this->apples;
  }
}
// Bar also extends FooBase, and inherits the same 3 properties
class Bar extends FooBase {
  private $variable_three;
  private $variable_four;

  public function __construct() {
    $this->oranges = 9;
  }
  public function getOranges() {
    echo "I have {$this->oranges} oranges too!";
  }
}
于 2012-10-10T01:31:57.720 回答
2

您可以使用继承

class FruitObject {
    protected $apples;
    protected $bananas;
    // ...
}

class Foo extends FruitObject {
    private $var1;
    private $var2;
}

class Bar extends FruitObject {
    private $var1;
    private $var2;
}
于 2012-10-10T01:34:04.827 回答
0

仅仅因为您使用的是 OOP 并不意味着您不能使用数组来存储不同的类型。这样,在进入实际实现之前,除了继承之外,您不必担心声明所有不同类型的属性。

// The base class defines common properties
class FooBase {
  // protected properties will be available to extending classes
  protected $fruit=array();
}
// Foo extends FooBase, inheriting its protected & public properties
class Foo extends FooBase {
  private $variable_one;
  private $variable_two;
  protected $fruitType;

  public function __construct() {
    //declare a fruitType
    $this->fruitType = 'apples';  
    // Initialize some stuff
    $this->fruit[$this->fruitType] = 3;
    //test it on load
    $this->getFruit();
  }
  public function getFruit() {
    // $this->fruit inherited from FooBase
    echo $this->fruit[$this->fruitType];
  }
}
// Bar also extends FooBase, and inherits the same 3 properties
class Bar extends FooBase {
  private $variable_three;
  private $variable_four;
  protected $fruitType;

  public function __construct() {
    $this->fruitType = 'oranges';
    $this->fruit[$this->fruitType] = 9;
    $this->getOtherFruit();
  }
  public function getOtherFruit() {
    echo "I have {$this->fruit[$this->fruitType]} {$this->fruitType} too!";
  }
}
于 2017-10-19T07:00:24.727 回答