-1

I am wondering how to get the following to be a global function. I want the class function to not have to keep logging into the MySQL I am using

https://github.com/joshcam/PHP-MySQLi-Database-Class#initialization

This is what I have done so far, would that work?

<?php
require_once('system/MysqliDb.php');
$db = new MysqliDb ('host', 'username', 'password', 'databaseName');

class system{
  public function __construct() {
    $db;
 }

}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

2 Answers2

1

Pass in the database object using "dependency injection"

class system {
  protected $_db;
  public function __construct(MysqliDb $db){
    $this->_db = $db;
  }
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Spechal
  • 2,634
  • 6
  • 32
  • 48
  • Sorry but that did not work the error I get is follows [Fri Jun 05 14:32:47 2015] [error] [client 49.199.36.167] PHP Catchable fatal error: Argument 1 passed to system::__construct() must be an instance of MysqliDb, none given, called in /home/my$ – RussellHarrower Jun 05 '15 at 18:31
  • That means you didn't pass it anything. Do you call it as new system($db) ... ? – Spechal Jun 05 '15 at 19:47
0

I would suggest to use getInstance() method.

class system {
  protected $_db;
  public function __construct() {
    $this->_db = MysqliDb::getInstance();
  }
}
alex b
  • 24
  • 3