1

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

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
jeugen
  • 344
  • 4
  • 23
  • Please do not use global state to share DB connection between several instance. Instead use something like this: http://stackoverflow.com/a/11369679/727208 – tereško Apr 04 '14 at 15:07

2 Answers2

0

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();
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 1
    Reference from the [PHP docs](http://www.php.net/manual/en/language.oop5.properties.php): `This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. ` – Mark Baker Apr 04 '14 at 12:12
  • Thanks for that Mark , It gave a parse error as the line of code was outside of the `constructor`, so that must be run-time related right ? – Shankar Narayana Damodaran Apr 04 '14 at 12:19
  • The fact that it needs to ___execute___ `parent::connection();` to get the value to assign means that it needs run-time information in order to be evaluated – Mark Baker Apr 04 '14 at 12:21
  • Thank you Mark , Any idea why was my answer downvoted ? – Shankar Narayana Damodaran Apr 04 '14 at 12:23
  • So Im a beginner, but if I got the point, parent::connection() should be executed, because it returns the connection, right? – jeugen Apr 04 '14 at 12:25
  • @user3497923, The first comment on this answer by Mark explains why you got a parse error. Yeah you are right `parent::connection()` returns you the connection. – Shankar Narayana Damodaran Apr 04 '14 at 12:27
  • 2
    @ShankarDamodaran - no idea why the downvote, I countered it by upvoting – Mark Baker Apr 04 '14 at 12:42
  • I didnt downvoted your answer, but I tried to upvote, but I dont have enough reps. Simple assignment public $database = parent::connection(); wont execute the method? – jeugen Apr 04 '14 at 12:48
  • Once again I have to say :) The answer lies in this [**comment**](http://stackoverflow.com/questions/22862399/how-can-i-use-variable-from-class-in-another-class/22862446?noredirect=1#comment34879576_22862446) by Mark. Let know if you find difficulty understanding it. – Shankar Narayana Damodaran Apr 04 '14 at 12:50
  • @ShankarDamodaran thank you for helping. Can you explain me, why cant I use **global $database;** inside the **DbPlus class** however I defined in the _initializing.php_ ? – jeugen Apr 04 '14 at 12:51
  • Because that breaks your `OOP` , Also , the singleton pattern you have used is a bad practice. Please read this beautiful article. http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it – Shankar Narayana Damodaran Apr 04 '14 at 12:56
  • @ShankarDamodaran Thank you, I will carefully read it, and make some practice. Do you have any idea(article, link['same_as_above'], book) how to keep alive db connection when with oop, without usin singleton? Thank you again – jeugen Apr 04 '14 at 13:03
  • You need to look up on [**Persistent Database Connections**](http://www.php.net/manual/en/features.persistent-connections.php#features.persistent-connections) – Shankar Narayana Damodaran Apr 04 '14 at 13:07
0

try something like this and let me know :

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

Also give us the PHP version you are using

Su4p
  • 865
  • 10
  • 24