There is a connection class to db:

class DataBase { public static $db = null; public $msqli; public static function getDb() { if (self::$db == null) { self::$db = new DataBase(); } return self::$db; } private function __construct() { $this->msqli = mysqli_connect('localhost', 'root', '', 'depsoft'); } public function __destruct() { if ($this->msqli) { $this->msqli->close(); } } } 

There is a class inheriting from it in another file that performs some other functions:

 require_once "connect.php"; class DateFormToInsert extends DataBase { public static function dateFromToInsert() { $query_user = mysqli_query($this->msqli, "SELECT * FROM `region_table`"); $data = mysqli_fetch_array($query_user, MYSQLI_ASSOC); echo "<pre>"; print_r($data); } } $aa = DateFormToInsert::getDb(); DateFormToInsert::dateFromToInsert(); 

Is this approach correct? Before that, I did not try to write to the op. And the second question is how to get access to $ this-> msqli defined in the parent in the child class?

    3 answers 3

    To start on the first class.

    1. public static $db = null; should be private , since you are doing the singleton and give it away by the method
    2. The destructor here, in principle, is not really needed. mysqlnd closes the connection itself at the end of the script
    3. Why do you need such a strange approach with $db and $mysqli ? Wouldn't it be better for getDb immediately give the connection resource to the database (and initialize it if necessary)?
    4. In getDb it would be nice to insert a connection error handling so as not to give false or null to where they are not expected.
    5. This is already a taste, but I would use PDO , not mysqli

    Now for the second class.

    No, this is not worth doing, because the two classes have very different responsibilities. It's like inheriting a grandfather from a shovel, because they are both old ones.

    And so it is necessary

     $query_user = mysqli_query(Database::getDb(), "SELECT * FROM `region_table`"); 

    Here your DataBase will be a dependency provider for DateFormToInsert

       class DB { private static $_instance = null; // для безопасности настройки лучше хранить в файле с конфигом private static DB_HOST = ''; private static DB_NAME = ''; private static DB_USER = ''; private static DB_PASS = ''; private function __construct () { $this->_instance = new PDO( 'mysql:host=' . self::DB_HOST . ';dbname=' . self::DB_NAME, self::DB_USER, self::DB_PASS, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"] ); } private function __clone () {} private function __wakeup () {} public static function getInstance() { if (self::$_instance != null) { return self::$_instance; } return new self; } } require_once "connect.php"; class DateFormToInsert { public static function dateFromToInsert() { $db = DB::getInstance(); $query_user = $db->prepare("SELECT * FROM `region_table`")->execute(); $result = $sth->fetchAll(); print_r($result); } } 
      • Actually, he's already singleton. - rjhdby
      • Well, actually, yes, and so singleton. And how to use it correctly in another file? And in general, what to change to get to the $ msqli variable? - Forever Young
      • in general it is not complete, since it can be cloned - madfan41k
      • Defective because I change access areas, because I can not solve the problem. And so there everything was private - Forever Young
      • What is the new DataBase () class responsible for? - madfan41k

      $ query_user = mysqli_query (DataBase :: getDb () -> msqli, "SELECT * FROM region_table ");