0

Why I get the message Fatal error: Call to undefined method database::query()
when I pass the same code to a class? here my code:

    class database{
    protected $databaseLink = null;
    function __construct($dbInfo){

        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        $this->database = $dbInfo['host'];
        $this->mysql_user = $dbInfo['user'];
        $this->mysql_pass = $dbInfo['pass'];
        $this->mysql_db = $dbInfo['dbname'];
        $this->openConnection();
        return $this->get_link();
    }
    function openConnection(){
      try {
           $this->databaseLink = new mysqli($this->database , $this->mysql_user, $this->mysql_pass, $this->mysql_db);
           $status = $this->databaseLink->set_charset("utf8");
           if ($status === false) {
             //throw new \Database_Exception(self::$dbInstance->error, self::$dbInstance->errno);
           }
      } catch (mysqli_sql_exception $e) {
              if (mysqli_connect_error()) {
                  die('Connect Error (' . mysqli_connect_errno() . ') '
                          . mysqli_connect_error());
              }
          //echo $e->__toString();
      }
    }
    function get_link(){
        return $this->databaseLink;
    }
}
$db = new database($dbInfo);  
$query = "SELECT prod_id as id, diction, name as display_name FROM htbl_products ORDER by id LIMIT 10";

$result = $db->query($query); //Fatal error: Call to undefined method database::query()

If I try the code without the class-wrapper like this:

try {
         $db = new mysqli($dbInfo['host'], $dbInfo['user'], $dbInfo['pass'], $dbInfo['dbname']);
         $db->set_charset("utf8");
    } catch (mysqli_sql_exception $e) {

            if (mysqli_connect_error()) {
                die('Connect Error (' . mysqli_connect_errno() . ') '
                        . mysqli_connect_error());
            }
        //echo $e->__toString();
    }

it works.

hamburger
  • 1,339
  • 4
  • 20
  • 41
  • 1
    Because `new database()` returns the database class, not the link. You cannot return values out of the constructor method: https://stackoverflow.com/questions/11904255/constructor-returning-value – aynber Dec 12 '17 at 18:59
  • if you want to be able to use the mysqli methods, you should probably take a look at [factories](https://stackoverflow.com/questions/2083424/what-is-a-factory-design-pattern-in-php). That way you can avoid the rabbit hole of trying to wrap every mysqli functionnality in your custom class. – William Perron Dec 12 '17 at 19:47
  • your "database" class does not have a method called "query", so you can't call it on an instance of that class. You'd need a declaration `function query() {` etc within the class, which does whatever you're expecting it do. Obviously it works "without the wrapper" because you're simply making your calls against a completely different class (mysqli). I think perhaps, as anyber hinted above, you've misunderstood how OO classes and constructors work. – ADyson Dec 12 '17 at 21:48

0 Answers0