【问题标题】:php Long list of properties in a class - can I shorten it?php 类中属性的长列表 - 我可以缩短它吗?
【发布时间】:2012-09-30 09:56:08
【问题描述】:

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

如何使我的文件更短,属性列表在发生更改时更灵活?

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

我有 class fooclass 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 行长,在两个类的前面都有这么长的简介相当烦人。

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

非常感谢!

【问题讨论】:

  • 只是一个想法:超级类...

标签: php oop class properties


【解决方案1】:

听起来这两个类都应该继承自定义公共属性的基类。查看PHP manual on object inheritance

// 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!";
  }
}

【讨论】:

    【解决方案2】:

    你可以使用inheritance:

    class FruitObject {
        protected $apples;
        protected $bananas;
        // ...
    }
    
    class Foo extends FruitObject {
        private $var1;
        private $var2;
    }
    
    class Bar extends FruitObject {
        private $var1;
        private $var2;
    }
    

    【讨论】:

      【解决方案3】:

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

      // 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!";
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        • 2020-11-05
        • 1970-01-01
        相关资源
        最近更新 更多