【问题标题】:Declare an Abstract class in php在 php 中声明一个抽象类
【发布时间】:2017-03-05 10:24:13
【问题描述】:

我有 2 个班级 - A 和 B 两者都应该使用来自 .ini 或 .php 的配置文件。 并且都有方法调用 post 从不同的 api 获取 smth 最好的设计方法是什么?

我是否需要使用抽象并将配置放入他的构造函数中。 ? 我只需调用 $this->config?

就可以获取配置

例如:

abstract class parent
{
  constructor()
   {
   $this->config = "get file";
}
}

class a extends parent {
   ...
   how to use $this->conifg ? 

}

非常感谢

【问题讨论】:

    标签: php abstract


    【解决方案1】:

    只需在你的父类中定义一个函数,它会返回你的配置:

    abstract class parent
    {
    
       private config;
    
       public function __construct()
       {
          $this->config = "get file";
       }
    
       public function getConfig(){
          return $this->config;
       }
    
    }
    
    class a extends parent
    {
    
       private config;
    
       public function __construct(){
          $this->config = parent::getConfig();
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 2013-06-19
      • 2015-11-24
      • 2016-05-10
      • 2014-02-26
      • 1970-01-01
      • 2021-06-10
      • 2013-07-30
      相关资源
      最近更新 更多