0

I had 2 Class in PHP That i want to use with each other, but the Class in 2 different PHP Script like clothing_product.php and database.php. It look like this Below:

database.php:

require_once('config.php');

class MySqlDatabase
{
    private $connection;
    private $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exist;

        function __construct(){
            $this->open_connection();
            $this->magic_quotes_active = get_magic_quotes_gpc();
            $this->real_escape_string_exist = function_exists("mysql_real_escape_string");

        }

        private function open_connection()
        {

            $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
            if (!$this->connection){
                die("Connection failed!". mysql_error());
            }
            else{
                $db_select = mysql_select_db(DB_NAME);
                if(!$db_select){
                    die("Select database failed". mysql_error());
                }
            }

        }

        public function query($sql){

            $this->last_query = $sql;
            $result = mysql_query($sql,$this->connection);

            $this->confirm_query($result);
            return $result;

        }

        public function confirm_query($result){
            if(!$result){
                $output = "Database query failed: " . mysql_error()."<br /><br />";
                $output.= "Last query that fail is:" . $this->last_query;
                die($output);
            }
        }

        private function escape_value($value) {

            if ($this->real_escape_string_exist) {
                if($this->magic_quotes_active) {$value = stripslashes($value);}
                $value = mysql_real_escape_string($value);
            }
            else {
                if (!$this->magic_quotes_active) {$value = addslashes($value);}
            }
            return $value;
        }

        public function fect_array($result){
            return mysql_fetch_array($result);
        }

        public function num_rows($result){
            return mysql_num_rows($result);
        }

        public function last_id(){
            return mysql_insert_id($this->connection);
        }

        public function affected_rows(){
            return mysql_affected_rows($this->connection);
        }

        public function close_connection(){
            if(isset($this->connection)){
                mysql_close($this->connection);
                unset($this->connection);
            }
        }

}

//$db = new MySqlDatabase();

clothing_product.php:

include('../database.php');

class Clothing_Product {

    public $db = new MySqlDatabase();

        public static function test(){
            echo "Static call successfull";
            return "Static call successfull";
        }


    }

The problem is when i try to USE 'Public $db = new MySqlDatabase();' in class clothing_product i get Error. I think the problem is maybe i got a wrong call. Please help me cuz m a noob thnk.

1 Answer 1

2

You can't initialize member variables to anything that is not static, and you're trying to create an object here:

public $db = new MySqlDatabase();

From the manual:

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.

The workaround is to set your variable in the constructor:

public $db;
public function __construct() { 
    $this->db = new MySqlDatabase();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can i use $db in this way ? public static function test(){$this->ds->open_connection(); echo "Static call successfull"; return "Static call successfull"; }
@Bong - No, because it's named db not ds, and open_connection is private in the MySqlDatabase class. You don't need to call open_connection() yourself. It should be: public function test(){ $this->db->query( "whatever"); }. Except you can't keep this function static, because you need to instantiate the class (so the constructor is called).
really thank you for the help write wrong about the ds sorry for that anyway, you make me very clear that we cant use the OBJ with that static, so that i need to instantiate my OBJ in real time to make this work and use it with none static. It really helpful thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.