Question

I wants to call methods in a class, using db connection from another class.

initializing.php

// load the configs, AFTER this I SHOULD HAVE CONSTANTS
require_once('myconfigs.php');

// load some function , some HELPER
require_once(LIB_PATH.DS.'functions.php');

// load the core objects
require_once(LIB_PATH.DS.'database.php');
$database = MySQLDatabaseConnection::connection();

the database.php

require_once('myconfigs.php');

require_once(LIB_PATH.DS.'initializing.php');

class MySQLDatabaseConnection {

    private static $instance;

    private function __construct(){

    }

    private function __clone(){

    }

    public static function connection(){

        if ( !isset( self::$instance ) ) {
            self::$instance = new MySQLi(DBSERVER, DBUSER, DBPASSWORD, DBNAME);
            if ( self::$instance->connect_error ) {
                throw new Exception('MySQL connection failed: '. self::$instance->connect_error);
            }
        }

        return self::$instance;

    }



}

the listproducts.php

It is for some testing, after this in DbPlus class I want to create CRUD methods, but looks like simple echo doesnt works :/

require_once('initializing.php');

class DbPlus extends MySQLDatabaseConnection{

    public $database = parent::connection();

    public function read(){
        $query = "SELECT name, price_in, price_out, category_products_id FROM products";
        $data = $database->query($query);

        while ( $row = $data->fetch_object()) {
            echo $row->name;
        }
    }

}

$dbplus = new DbPLus;
$dbplus->read();

Im getting this error

Parse error: syntax error, unexpected '(', expecting ',' or ';' in D:\XAMPP\htdocs\xxx\includes\listproducts.php on line 19

I dont have clue where does unexpected '(' comes from.

Any idea? Thank you

Was it helpful?

Solution

Some serious problems with your listproducts.php

You should do this assignment on a constructor

public $database = parent::connection(); //<--- This gave you a syntax error.

Like this...

  public $database;
    function __construct()
    {
     $this->database = parent::connection();
    }

Next thing comes your read() , you need to make use of $this keyword..

Should be..

public function read(){
        $query = "SELECT name, price_in, price_out, category_products_id FROM products";
        $data = $this->database->query($query);   //<---- Do this way..

The fixed code..

<?php
require_once('initializing.php');

class DbPlus extends MySQLDatabaseConnection{

    public $database;
    function __construct()
    {
     $this->database = parent::connection();
    }

    public function read(){
        $query = "SELECT name, price_in, price_out, category_products_id FROM products";
        $data = $this->database->query($query);

        while ( $row = $data->fetch_object()) {
            echo $row->name;
        }
    }

}

$dbplus = new DbPLus;
$dbplus->read();

OTHER TIPS

try something like this and let me know :

$dbplus = new DbPLus();
$dbplus->read();

Also give us the PHP version you are using

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top