Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不能将类对象与其他类一起使用_Php_Oop_Class_Static Methods - Fatal编程技术网

Php 不能将类对象与其他类一起使用

Php 不能将类对象与其他类一起使用,php,oop,class,static-methods,Php,Oop,Class,Static Methods,我有两个PHP类,我想彼此使用,但是这个类在两个不同的PHP脚本中,比如towning_product.PHP和database.PHP。如下所示: database.php: require_once('config.php'); class MySqlDatabase { private $connection; private $last_query; private $magic_quotes_active; private $real_escape_s

我有两个PHP类,我想彼此使用,但是这个类在两个不同的PHP脚本中,比如towning_product.PHP和database.PHP。如下所示:

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();

问题是当我尝试使用“Public$db=new MySqlDatabase();”时在课堂上,我得到了一个错误。我想问题是我可能打错电话了。请帮帮我,因为我是个傻瓜

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

public $db = new MySqlDatabase();
发件人:

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

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

public $db;
public function __construct() { 
    $this->db = new MySqlDatabase();
}

我可以这样使用$db吗?public static function test(){$this->ds->open_connection();echo“static call successfull”;return“static call successfull”}@Bong-No,因为它名为
db
not
ds
,而
open_connection
MySqlDatabase
类中是
private
。您不需要自己调用
open\u connection()
。它应该是:
public function test(){$this->db->query(“whatever”);}
。除了你不能让这个函数保持静态之外,因为你需要实例化这个类(因此调用构造函数)。真的非常感谢你的帮助,写了关于ds的错误。很抱歉,无论如何,你让我非常清楚,我们不能用这个静态的OBJ,因此,我需要实时实例化我的OBJ,以使其正常工作,并在无静态的情况下使用它。这真的很有帮助,再次谢谢你。
public $db;
public function __construct() { 
    $this->db = new MySqlDatabase();
}