Jump to content

Class Question


N-Bomb(Nerd)

Recommended Posts

Hi,

 

I want to connect to my database with mysqli at the start of my class and be able to use that connection throughout my class without reconnecting again. I'm still pretty new to classes, but I can't figure this out..

 

I tried doing this in the in the construct, but it didn't work:

$this->connection = new mysqli("localhost", "username", "password", "database");

How can I access my connection to the database anywhere in my class without reconnect?  :confused:

 

Thanks.

Link to comment
Share on other sites

Your constructor should never do real work only initialization logic:

 

class SomeClass {
    private $_db;
    public function __construct($value) { $this->_value = $value;/* <-- no work */ }
    public function getDb() {
        if (null === $this->_db) {
            $this->_db = new MySQLi(...);
        }
        return $this->_db;
    }
}

 

When you are programming remember to always lazy-load everything hold the creation of an object off until you absolutly need it.

Link to comment
Share on other sites

you need a singleton  or helper function...

 

helper

function getDB ()
{
if(!isset($conn))
{
  $conn = new mysqli("localhost", "username", "password", "database");
}
return $conn;
}
////

then anywhre in your code where you need database just:

$db = getDB();
$db->query("SELECT * FROM `foo`");
?>

singleton class...

<?php
class DBConn
{

static $instance;
private $_conn;

private function __construct()
{
  $this->conn = new mysqli("localhost", "username", "password", "database");
}

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

/////

then anywhere in your code:

$db = DBConn::getInstance;

$db->query("SELECT * FROM `bar`);
?>

 

there is an over head so in most cases you would probably lean toward the helper function method...

Link to comment
Share on other sites

The singleton can be a very useful design pattern...

 

In terms of databases and where you may need a second database connection yes it will not be what you want...

 

To get round that you can use an array in the class that stores the connection string (host,username,passord,database) and when called again see if the current connection string already exists - if so return that connection if not open a new one and return that.


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.