Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP类-样板文件还是继承?_Php_Oop_Inheritance_Boilerplate - Fatal编程技术网

PHP类-样板文件还是继承?

PHP类-样板文件还是继承?,php,oop,inheritance,boilerplate,Php,Oop,Inheritance,Boilerplate,免责声明:我有点像个noob,只做了不到一年的PHP,而做OO-PHP的还不到一年 我正在编写几个类,它们都有一些相同的功能。这是我的样板代码: class ClassName { // required constructor arguments private $arg1; // optional arguments in an array. $options array specifies their names // and required types. '

免责声明:我有点像个noob,只做了不到一年的PHP,而做OO-PHP的还不到一年

我正在编写几个类,它们都有一些相同的功能。这是我的样板代码:

class ClassName {

   // required constructor arguments
   private $arg1;

   // optional arguments in an array. $options array specifies their names
   // and required types. '' means any type.
   private $options = array('option1'=>'type', 'option2'=>'');
   private $option1 = 'default_value';
   private $option2 = 'default_value';

   /* getters and setters would go here if I wanted them */

   // this would probably change after debugging
   public function __toString() {
      return print_r(get_object_vars($this), true); 
   }

   public function __construct($arg1, array $options = array()) {
      // set all required args
      $this->arg1 = $arg1;
      // cycle through $options array, check they are allowed,
      // and check their type
      foreach ($options as $option => $value) {
         $type = $this->options[$option]; // no value = any type is OK
         if (array_key_exists($option, $this->options) 
                && (gettype($value) === $type || !$type)) {
            $this->$option = $value;
         }
      }
   }

   // methods go here
}
我一直在使用这种格式:在数组中使用必需的参数,然后是可选的参数,使用foreach循环分配所有可选变量,将选项及其类型(我关心的主要区别是数组与非数组)指定为私有变量

检查和分配每个可选参数的foreach循环不会更改。我可以复制并粘贴它来创建新类,但我也认为这样做可能会更好,以避免重复代码:

abstract class ParentClass {

   public function __toString() {
      return print_r(get_object_vars($this), true); 
   }

   protected function setOptions($options) {
      foreach ($options as $option => $value) {
         $type = $this->options[$option]; // no value = any type is OK
         if (array_key_exists($option, $this->options) 
                 && (gettype($value) === $type || !$type)) {
            $this->$option = $value;
         }
      }
   }
}

class ChildClass extends ParentClass{

   private $arg1;
   private $arg2;
   private $options = array('option1'=>'string', 'option2'=>'array');
   private $option1 = 'default_value';
   private $option2 = array('foo', 'bar');

   public function __construct($arg1, $arg2, $options = array()) {
      $this->arg1 = $arg1;
      $this->arg2 = $arg2;

      parent::setOptions($options);

   }
}
我还没有对继承做太多的工作。这是它的一个好用途吗


谢谢

这将是继承的一个很好的用途,而且减少重复代码始终是最佳实践。将来当需要更改或出现错误时,代码修改起来就不那么麻烦了

编辑:顺便说一下,您还可以将所有的构造逻辑放在父类的构造函数中,然后在子类中重写它,并在处理完特定于类的逻辑后调用父构造函数。例如:

abstract class ParentClass {
   public function __construct($options) {
      foreach ($options as $option => $value) {
         $type = $this->options[$option]; // no value = any type is OK
         if (array_key_exists($option, $this->options) 
         && (gettype($value) === $type || !$type)) {
            $this->$option = $value;
         }
      }
   }
   public function __toString() {
      return print_r(get_object_vars($this), true); 
   }
}

class ChildClass extends ParentClass{
   private $arg1;
   private $arg2;
   private $options = array('option1'=>'string', 'option2'=>'array');
   private $option1 = 'default_value';
   private $option2 = array('foo', 'bar');

   public function __construct($arg1, $arg2, $options = array()) {
      $this->arg1 = $arg1;
      $this->arg2 = $arg2;

      parent::__construct($options);
   }
}

啊,对。实际上,如果我不调用父构造函数,我的IDE会向我抱怨。