Error when using __construct in a child of a MySQL class (it is solved by not using __construct but I need it if or if)


I have two classes: The first is called "MySQL" and contains all the functions for the queries (in addition to the ones that the API "MySQLi" brings.) Use __construct to create the MySQLi object. The second (Users), which is the daughter of the previous one, uses a __construct to instantiate other necessary classes.

When using __construct in the "Users" class, it returns an error: Fatal error: Uncaught Error: Call to a member function query () on null in D: \ Lautaro \ Applications \ Programs \ Xampp \ htdocs \ src \ classes \ MySQL.php.

If I delete __construct and its sentences, the error is solved, but I need YES or SI to make those instances.


namespace Brax\BBDD;

  require_once $_SERVER['DOCUMENT_ROOT'].'/config/cfg.php';

  class MySQL {
    private $server;
    private $username;
    private $password;
    private $database;
    public $mysqli; // Ver si la visibilidad afecta a los traits

    public function __construct() {
      $this->server   = 'localhost';
      $this->username = 'root';
      $this->password = '';
      $this->database = 'db';

      $this->mysqli = new \mysqli($this->server, $this->username, $this->password, $this->database);

     * This function will make a query to the DB.
     * @param  string $sql       [It's a SQL string]
     * @return object|false      [If succesfull, it will return an object, else, it will return false]
    protected function query(string $sql) {
      if (!empty($sql)) {
        if ($response = $this->mysqli->query($sql)) {
          if (DEBUG === true) {
            echo '['.HOUR.'] MySQLi query() DEBUG [SQL Executed]: '.$sql.PHP_EOL;

          return $response;
        } else {
          if (DEBUG === true) {
            echo '['.HOUR.'] MySQLi query() DEBUG [SQL Executed with ERROR * ]: '.$sql.PHP_EOL;

          return false;
      } else {
        if (DEBUG === true) {
          echo '['.HOUR.'] MySQLi Query DEBUG [SQL]: The query is empty.'.PHP_EOL;

        return false;


namespace Brax\Usuarios;
  require_once $_SERVER['DOCUMENT_ROOT'].'/config/cfg.php';

  class Usuarios extends \Brax\BBDD\MySQL {
    use \Brax\Traits\CRUDTrait;

    private $Panel,

    public function __construct() {
      $this->Panel       = new \Brax\Panel\Panel();
      $this->Accesos     = new Accesos\Accesos();
      $this->Referidos   = new \Brax\Referidos\PostsReferidos();
      $this->GameServers = new \Brax\GameServers\GameServers();

The Users class should work with __construct, and not return the error explained above.

asked by Lautaro Aguirre 04.01.2019 в 03:24

1 answer


The documentation on extends is not totally clear about the father-son relationship and what happens with its constructors.

If the child does not have a constructor, the constructor of the father is called .. but if the son has a constructor ... there is the problem.

Below, in one of the comments , it is much clearer what the problem is .

It turns out that you have to explicitly call the father (as is done in some languages, some make the call implicitly), to get it to run (and also, according to the comments, to see all the variables)

So, to your code, the following line in the son's constructor is missing:


In the documentation on constructors , he specifically clarifies it:




PHP does not call constructors of the base class automatically from to   builder of a derived class. It is your responsibility to propagate   the call to constructors upstream where appropriate.

answered by 04.01.2019 / 03:37