简体   繁体   English

如何在PHP的所有子实例中仅一次调用父构造函数

[英]how to call parent constructor just once in all child instances in php

I am trying to develop an object oriented PHP application in which whole php application will be extending from MyApplicationBase base class. 我正在尝试开发一个面向对象的PHP应用程序,其中整个PHP应用程序将从MyApplicationBase基类扩展。 But the problems is I want to create only single instance of MyApplicationBase. 但是问题是我只想创建MyApplicationBase的单个实例。 Below is the code which explains what I mean 下面的代码解释了我的意思

class MyApplicationBase{

    static $called=0;
        public var $db;
    function __construct()
    {
        self::$called++;
        echo "<pre>MyApplicationBase Created ".self::$called." times</pre>";        
                $this->db=new DatabaseWrapper();
    }
}

class ApplicationSecurity extends MyApplicationBase{

       function is_logged_in()
       {
            $res=$this->db->query("user check db query goes here");
            return ($res)?true:false;
       }
       //..... other methods related to ApplicationSecurity class
}

class ApplicationBusinessLogic extends MyApplicationBase{

     // business logic methods here which may use base class vars like $db
     // this may also use instance of ApplicationSecurity class
}

class ApplicationTemplating extends MyApplicationBase{

      protected function outputHeader()
      {
         require_once('path/to/themes/header.php');
      }
      protected function outputSidebar()
      {
         require_once('path/to/themes/siderbar.php');
      }
      protected function outputMainbody()
      {
        require_once('path/to/themes/mainbody.php');
        $app=new ApplicationBusinessLogic();
        $app->initiate();
      }
      protected function outputFooter()
      {
         require_once('path/to/themes/footer.php');
      }
      public function outputTemplate()
      {
         $this->outputHeader();
         $this->outputSidebar();
         $this->outputMainbody();
         $this->outputFooter();
      }
}

//index.php file code starts here--------
$myPhpApplication = new ApplicationTemplating();
$myPhpApplication->outputTemplate();

My goal is when I create instance of my application then It only call the single instance of "MyApplicationBase" class instead of calling it multiple times. 我的目标是当我创建应用程序实例时,它仅调用“ MyApplicationBase”类的单个实例,而不是多次调用它。 Please do tell me how can I achieve this. 请告诉我如何实现此目标。 I am google for 5 hours but unable to find any solution yet. 我在Google呆了5个小时,但找不到任何解决方案。

I am trying to develop an object oriented PHP application in which whole php application will be extending from MyApplicationBase base class. 我正在尝试开发一个面向对象的PHP应用程序,其中整个PHP应用程序将从MyApplicationBase基类扩展。

As PHP has single inheritance, this is by far the most worst idea to do object oriented PHP programming. 由于PHP具有单一继承,因此这是进行面向对象的PHP编程的最糟糕的主意。

But the problems is I want to create only single instance of MyApplicationBase. 但是问题是我只想创建MyApplicationBase的单个实例。

As every class is a MyApplicationBase you actually don't want that because it would mean you could instantiate exactly one class in your whole application. 由于每个类都是MyApplicationBase,因此您实际上不希望这样做,因为这意味着您可以在整个应用程序中实例化一个类。

What you're probably looking for is some kind of ApplicationClass which you pass along and of which just a single instance exists. 您可能正在寻找的是传递的某种ApplicationClass,其中只有一个实例。

This would at least allow you in the future to throw such a "block in road" away more easily then if you would have got extended from there. 至少,如果您从那里扩展了,那么将来至少可以让您更轻松地抛弃这种“障碍”。

In any case you should program against an ApplicationInterface instead of an ApplicationClass to make this throwing away - as it will be necessary - easier. 在任何情况下,您都应该针对ApplicationInterface而不是ApplicationClass进行编程,以简化此操作(这将是必要的)。

The best thing for sure would be to not do anything in that direction and only write code you need in the first place. 当然,最好的办法是不朝那个方向做任何事情,而只是首先编写您需要的代码。

To only write code you need, you need to develop test-driven. 为了只编写所需的代码,需要开发测试驱动的。 Why not start with that if you want to do object oriented programming? 如果要进行面向对象的编程,为什么不从此开始呢?

Well I suppose that you want to avoid multiple connections to the database in this case. 好吧,我想您想在这种情况下避免与数据库的多个连接。 Solution is simple with Dependency injection, just initialize your database connection outside of MyApplicationBase class and then pass it as a constructor parameter (beware of constuctor hell though). 解决方案使用依赖注入很简单,只需在MyApplicationBase类之外初始化数据库连接,然后将其作为构造函数参数传递(尽管要注意构造器地狱)。 Like this: 像这样:

class MyApplicationBase{

    static $called=0;
    public $db;
    function __construct($db)
    {
        self::$called++;
        echo "<pre>MyApplicationBase Created ".self::$called." times</pre>";        
                $this->db= $d;
    }
}

$db = new DatabaseWrapper();
$templating = new ApplicationTemplating($db);
$security = new ApplicationSecurity($db);

You could also take a look at some framework, they usually come with some dependency injection capabilities. 您也可以看看一些框架,它们通常带有一些依赖注入功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PHP中的子构造函数之前调用父构造函数 - Call parent constructor before child constructor in PHP 是否必须从PHP中的子类的构造函数中调用parent :: __构造? - Is it mandatory to call parent::__construct from the constructor of child class in PHP? PHP:从父方法中的静态方法调用子构造函数 - PHP: call child constructor from static method in parent 子类构造函数将如何与php中的父类构造函数交互? - How will a child class constructor interact with a parent class constructor in php? PhpStorm:如何生成具有所有父属性和子属性的构造函数 - PhpStorm: how to generate constructor with all parent properties and child too PHP-如果我们在php中创建子类的对象,它会调用父类构造函数,然后调用子类构造函数(如java)吗? - PHP -If we create object of child class in php, does it call parent class constructor and then child class constructor like java? PHP / MySQL,如何仅更新数据库中的一些记录,而不是一次全部更新? - PHP/MySQL, How to update just some records in database, not all at once? 如何让PHP类构造函数调用其父级的父构造函数 - How do I get a PHP class constructor to call its parent's parent's constructor Laravel 4 - 子构造函数通过依赖注入调用父构造函数 - Laravel 4 - Child constructor call parent constructor with dependency injection php-仅对所有用户执行一次功能 - php - execute a function just once for all users
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM