singleton - Loading a php class once

802

I am trying to get the php classBase to load only once for a custom MVC framework. So that i can close the database connection in the destructor and be able to use it in inheritance but I can't seem to figure it out. I looked at a singleton design here but i cant figure out how to load the database. Is there a way to get this working or perhaps a better way to do this. Or am i going about this in a totally stupid way and should change it?

Base class

class Base {
    protected $db;
    //constructor
    public function Base(){
        echo some unique id to test it worked
        $db = connection to database
    }

    //load modules as needed
    public function load($m){
        $this->$m = new $m;
    }
}

module class

class module extends Base {
    //some random function
    public function listing(){
        $this->db->query();
    }
}

the index.php which initializes the Base class

 $main = new Base;
 $main->load( 'module' );
 $main->module->listing();

People are also looking for solutions to the problem: mysql - How to find last inserted id , transaction, commit, rollback from the following connection php/MySQLi

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.