D
D
Dmitry2017-04-22 12:46:23
PHP
Dmitry, 2017-04-22 12:46:23

How to exclude file connection?

Hello, there is a class for working with a database

<?php
namespace model;
include_once 'config.php';


class Database
{
  private $host = HOST;
    private $user = USER;
    private $pass = PASS;
    private $database = DATABASE;
    private $db = NULL;
    private $sl = NULL;
    public $result = NULL;
    public $query = NULL;
    
    public function __construct()
    {
        $this->db = new mysqli(HOST, USER, PASS, DATABASE);
        if(!$this->db){
            echo "Невозможно установить соединение с базой данных<br/>Код ошибки:<br/>";
        	exit(mysql_error());
        }
        else
        {
        	mysqli_set_charset($this->db, 'utf8');
        }
    }
}

And there is a function __autoload
define('DIRSEP', DIRECTORY_SEPARATOR);

function __autoload($class_name) {
  $class_peaces = explode('\\', $class_name);

  switch ($class_peaces[0]) {
    case 'model':

          $dirpath = ROOT . DIRSEP . implode(DIRSEP, $class_peaces) . '.php';
          require_once $dirpath;
          break;
  }
}

The problem is that when you include a file with the Database( ) class, it tries to automatically include the mysqli.php class as well.$db = new model\Database();

Warning: require_once(W:/domains/testPHPoop\model\mysqli.php): failed to open stream: No such file or directory in W:\domains\testPHPoop\functions.php on line 15
Fatal error: require_once(): Failed opening required 'W:/domains/testPHPoop\model\mysqli.php' (include_path='.;w:/modules/php/PHP-5.4;w:/modules/php/PHP-5.4/PEAR/pear') in W:\domains\testPHPoop\functions.php on line 15

How to avoid including this non-existent file?
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy Odinets, 2017-04-22
@ddimonn8080

$this->db = new \mysqli(HOST, USER, PASS, DATABASE);

You have a namespace model specified, and the script tries to find the class model\mysqli. So you need to load mysqli from the global namespace .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question