简体   繁体   中英

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. But the problems is I want to create only single instance of 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. Please do tell me how can I achieve this. I am google for 5 hours but unable to find any solution yet.

I am trying to develop an object oriented PHP application in which whole php application will be extending from MyApplicationBase base class.

As PHP has single inheritance, this is by far the most worst idea to do object oriented PHP programming.

But the problems is I want to create only single instance of 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.

What you're probably looking for is some kind of ApplicationClass which you pass along and of which just a single instance exists.

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.

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM