Вопрос

I don't know if there is someting like that but i searched for more than 5 hours with no positive results.

I have class called DataBase which have a function called connect that connects to database

what i'm tring to do is to call method "connect" automatically without calling it each time in my extended classes

But it didn't work correctly,

What i really did , i called connect function in my extended class but it tells me that there is no database selected

here is an example

my database php file

include_once "config.php";
class DataBase {
  private $_localhost;
  private $_user;
  private $_password;
  private $_dbname;
  protected $db;

  function __construct($localhost,$user,$password,$dbname){

    $this->_localhost = $localhost;
    $this->_user      = $user;
    $this->_password  = $password;
    $this->_dbname    = $dbname;
    $this->connect();
 }

 function connect(){
   try{
    $this->db = new PDO("mysql:host=".$this->_localhost.";dbname=".$this->_dbname,$this->_user,$this->_password);
    $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    }catch(PDOException $e){
      echo $e->getMessage();
    }

  }

  function close(){
    $this->db = null;
  }

}

$db = new DataBase($localhost,$user,$password,$dbname);

My queries php file

class display extends DataBase{
  private $tbname;
  private $sql;
  private $query;
  private $show_data;

  function __construct($tbname){

    $this->tbname = $tbname;
    parent::connect();

  }
  function getLastRow(){

      $this->sql = "SELECT * FROM ".$this->tbname." ORDER BY id DESC LIMIT 1";

      $this->query = $this->db->prepare($this->sql);

      try{
         $this->query->execute();
       }catch(PDOException $e){
         echo $e->getMessage();
      }    
      $this->show_data = $this->query->fetch();

      if($this->show_data){
      return $this->show_data;
      }

  }
}

now when i try to make new display in any of my controller files it gives me this error

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

As i said , what i want to do is to use this attribute $this->db in my extended classes with calling "connect" function automatically or something like tihs

Это было полезно?

Решение

How do you imagine that DataBase class will receive information about database, user, password and host?

You'll need to receive that information in the constructor of the child class and then call parent::__construct() with these arguments.

There's really no other way, except you prefer anti-patterns and bad design.

In your child class:

function __construct($localhost, $user, $password, $dbname, $tbname){
    $this->tbname = $tbname;
    parent::__construct($localhost, $user, $password, $dbname);
}

Actually it'd be better to just inject a DataBase instance into the display class instead of using inheritance here. You might want to reuse the same connection multiple times.

function __construct(DataBase $db, $tbname){
    $this->tbname = $tbname;
    $this->database = $db;
}
// you can now install some __call() wrapper in your DataBase class to proxy calls to $db property 

and in your main code

$db = new DataBase($localhost, $user, $password, $dbname);
$display = new display($db, $table);
// now you can reuse $db for other classes/direct use.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top