简体   繁体   中英

How to create a new class instance when variable is used

Its' hard to explain what i mean so here an example.

class mysql {

    public function __construct(){
        //connect to db and select table
    }
    ....
}


class Index Extends perent_class {

    public $mysql;

    public function no_mysql_required(){

        //this function might be called and no mysql is required 

    }

    public function mysql_required(){..}  // this needs mysql. 
    public function mysql_required2(){..} // this needs mysql.
    public function mysql_required3(){..} // this needs mysql.

}


Option 1:
 $this->mysql = new mysql();  
add this to all function that need the connection to mysql, but two of these function could be called and that would creates two connection to mysql which is bad.

Option 2:

 if( !is_object($this-mysql) ) $this->mysql = new Mysql();  
This will only create one mysql object at once and solves the problem, but it creates very repetitive code in the function(s). Creating & Calling a function $this->start_mysql() would be repetitive too.

So what i really want is whenever i call $this->mysql->function(); even tho its not created yet, to create the "$this->mysql = new Mysql" somehow in the Index perent_class automatically and once created, not to recreate it when a new function call is made. I might be doing this wrong and if there a better solution to what I'm doing, please post it.

Thank you.

What you want to do is use a singleton pattern for the mysql class. This way you don't have unnecessary connections open. So it would roughly look like this

Your mysql class

class Mysql{
    private function __construct(){ } 

    private static $instance;
    .
    .
 public static function singleton(){
        if (!isset(self::$instance)) {
            $c = __CLASS__; 
            self::$instance = new $c;
         }

       return self::$instance;
  }

And you would now use it like this in your other classes.

$mysql = Mysql::singleton(); 

If I understood your question correctly, it seems like you're wanting to use the 'Singleton' design pattern. This allows you to only create one instance of a class; attempting to create a second instance only returns the original instance. Here's a code extract:

<?php

class mysql {
  private static $mysql;

  public $testProperty;

  private function __construct() { } // Declared private so cannot instantiate objects in client code.

  public static function instance() {
    if (!isset(self::$mysql)) {
      self::$mysql = new self();
    }
    return self::$mysql;
   }
 }

?>

By using this pattern, a call to mysql::instance(); will return a new instance of the mysql object if one has not been created. If it has been created, it will return the same instance. Thus, you only ever have one instance of the object. Test it out by making several calls to mysql::instance() :

$instance1 = mysql::instance();
$instance1->testProperty = 'abc';
var_dump($instance1->testProperty);

$instance2 = mysql::instance();
var_dump($instance2->testProperty);

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