【问题标题】:Create new object in __constructor function PHP在 __constructor 函数 PHP 中创建新对象
【发布时间】:2014-11-05 05:44:20
【问题描述】:

我可以在 __constructor() 中创建一个新对象吗?所以我可以在当前类方法中使用该类。

假设我有这门课

class Config{

  public function configure($data){

  }
}

我想在一些Myclass 方法中使用Config,如下所示:

include 'Config.php'

class Myclass {
   function __construct(){
     $this->conf = new Config();   //just create one config object
   }

   public function method1($data){
     $this->conf->configure($data); //call the configure method
   }


   public function method2(){
     $this->conf->configure($data); //call again the configure method
   }

}

我可以像上面那样做吗?或者我必须像下面这样频繁地创建新对象:

class Myclass {

  public function method1($data){
    $this->conf = new Config(); //create config object
  }

  public function method2($data){
    $this->conf = new Config(); //create again config object
  }
}

由于我是编写自己的 php oop 代码的新手,我想知道当我想创建一个对象并在多个函数中使用它时哪种方法有效。谢谢!

【问题讨论】:

  • 了解特殊对象 $this。这里如果你使用 $this->conf 意味着你必须以非静态方式声明这个属性来获取它。即public $confprotected $confprivate $conf
  • 为什么不扩展Config 文件?
  • 抱歉,我已经编辑了我的问题。我没有扩展 Config,因为我只是想在某些函数中使用 Config 方法,而不是全部。
  • @MalikPerang - 无论您是扩展它还是创建它的实例,在任何情况下您都可以访问它的公共变量/方法。扩展还有一个好处是可以访问protected 属性。根据您的要求选择。
  • 哦,我明白了......谢谢你的想法,先生!

标签: php class oop object


【解决方案1】:

声明$conf first.try it -

include 'Config.php';

class Myclass {

   private $conf;
   function __construct(){
     $this->conf = new Config();   //just create one config object
   }

   public function method1($data){
     $this->conf->configure($data); //call the configure method
   }


   public function method2(){
     $this->conf->configure($data); //call again the configure method
   }

}

【讨论】:

    【解决方案2】:

    你可以试试

    protected $objDb;
    
    public function __construct() {
    $this->objDb = new Db();
    }
    

    请参考PHP DBconnection Class connection to use in another

    看看有没有帮助

    【讨论】:

    • 我的问题是如何在许多函数中使用单个声明的对象。而不是如何使用类连接到数据库。
    • 我已经在上面的代码中给出了引用,新对象在方法中被声明。您可以忽略数据库部分
    • 哦,现在我看到了你的帮助。谢谢!
    • 太好了..让我们看看它是否对您有帮助。
    【解决方案3】:

    你当然可以在构造函数中实例化一个新对象。您也可以将对象传递给它。

    class Foo
    {
        private $bar;
    
        public function __construct(BarInterface $bar)
        {
            $this->bar = $bar;
        }
    }
    

    围绕它有一个完整的概念,称为“依赖注入”。

    如果你像这样设计你的类,你总是可以为任何其他实现 BarInterface 的对象切换 $bar。

    【讨论】:

    • 啊哈“依赖注入”。我必须阅读更多关于此的内容。顺便说一句,谢谢分享!
    【解决方案4】:

    除了上面给出的解决方案之外,您还可以扩展它,使Config 文件成为超类。

    class Config{
      // create a constructor method
      public function __construct() {
        // some initialisation here
      }
    
      public function configure($data){
    
      }
    }
    

    然后你可以在你的代码中扩展这个类来使用inheritance

    include 'Config.php'
    
    class Myclass extends Config {
       function __construct(){
         parent::__construct();   //initialise the parent class
         // more initialisation
       }
    
       public function method1($data){
         $this->configure($data); //call the configure method
       }
    
    
       public function method2(){
         $this->configure($data); //call again the configure method
       }
    
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 2015-05-04
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多