У меня есть некоторые классы, которые должны подключаться к db и принимать общие переменные, поэтому я хотел бы задать вам некоторую информацию об этом.
Для db я попытался создать класс Singletone.
class Database { /** * @const Mysqli parameters */ const HOST = "xxxxx"; const USER = "xxxxx"; const PASS = "xxxxx"; const DB = "xxxxx"; /** * @var $instance The class instance */ private static $instance; /** * Class constructor * block directly instantiating */ private function __construct() {} /** * Class clone * block cloning of the object */ private function __clone() {} /** * Initialize Mysqli * create the instance if it does not exist * @return void */ public static function init() { if(!isset(self::$instance)) { self::$instance = new Mysqli(self::HOST, self::USER, self::PASS, self::DB); if(self::$instance->connect_error) { throw new Exception('MySQL connection failed: ' . self::$instance->connect_error); } } return self::$instance; } }
В других классах вызов класса Singletone таким образом
class Users { /** * @var $db init database class */ private $db; /** * Class constructor */ public function __construct() { $this->db = Database::init(); } /** * Simple mysql select */ public function simpleSQL() { $result = $this->db->query("SELECT * FROM users"); while($r = $result->fetch_assoc()) { echo $r['email'] . "<br />"; } } }
Теперь, что касается переменных (которые берутся db), я думал создать новый класс
class Config { /** * @var $db init database class */ private $db; /** * @var $public_ip * @type string */ public $public_ip; /** * @var $public_port * @type string */ public $public_port; /** * @var $manage_ip * @type string */ public $manage_ip; /** * @var $manage_port * @type string */ public $manage_port; /** * Class constructor */ public function __construct() { $this->db = Database::init(); $this->setVars(); } /** * Set variables "global" * @return @void */ public function setVars() { $result = $this->db->query("SELECT * FROM config WHERE id = 1"); while($r = $result->fetch_assoc()) { $this->public_ip = $r['public_ip']; $this->public_port = $r['public_port']; $this->manage_ip = $r['manage_ip']; $this->manage_port = $r['manage_port']; } } }
и затем инициализировать его в других классах таким образом
class Users { private $db; private $cnf; /** * Class constructor */ public function __construct() { $this->db = Database::init(); $this->cnf = new Config(); } /** * Simple mysql select */ public function simpleSQL() { $result = $this->db->query("SELECT * FROM users"); while($r = $result->fetch_assoc()) { echo $r['email'] . "<br />"; } } /** * Get vars */ public function getVars() { echo $this->cnf->public_ip . "<br />"; echo $this->cnf->public_port . "<br />"; echo $this->cnf->manage_ip . "<br />"; echo $this->cnf->manage_port . "<br />"; } } class foo { private $db; private $cnf; /** * Class constructor */ public function __construct() { $this->db = Database::init(); $this->cnf = new Config(); } /** * Get vars */ public function getVars() { echo $this->cnf->public_ip . "<br />"; echo $this->cnf->public_port . "<br />"; echo $this->cnf->manage_ip . "<br />"; echo $this->cnf->manage_port . "<br />"; } }
благодаря