0

我有 2 个 PHP 类,我想相互使用,但是 2 个不同的 PHP 脚本中的类,比如clothing_product.php 和 database.php。如下所示:

数据库.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();

服装产品.php:

包括('../database.php');

class Clothing_Product {

    public $db = new MySqlDatabase();

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


    }

问题是当我尝试使用'Public $db = new MySqlDatabase();' 在类clothing_product 我得到错误。我认为问题可能是我打错电话了。请帮助我,因为我是菜鸟。

4

1 回答 1

2

您不能将成员变量初始化为非静态的任何内容,并且您正在尝试在此处创建一个对象:

public $db = new MySqlDatabase();

手册

这个声明可能包括一个初始化,但这个初始化必须是一个常量值——也就是说,它必须能够在编译时被评估,并且不能依赖于运行时信息才能被评估。

解决方法是在构造函数中设置变量:

public $db;
public function __construct() { 
    $this->db = new MySqlDatabase();
}
于 2013-01-23T14:21:53.477 回答