【问题标题】:Cant use the class object with other class不能将类对象与其他类一起使用
【发布时间】:2013-01-23 14:18:16
【问题描述】:

我有 2 个 PHP 类,我想相互使用,但类在 2 个不同的 PHP 脚本中,如服装_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();

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";
        }


    }

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

【问题讨论】:

    标签: php oop class static-methods


    【解决方案1】:

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

    public $db = new MySqlDatabase();
    

    来自the manual

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

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

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

    【讨论】:

    • 我可以这样使用$db吗?公共静态函数 test(){$this->ds->open_connection(); echo "静态调用成功"; return "静态调用成功"; }
    • @Bong - 不,因为它被命名为db 而不是ds,并且open_connectionMySqlDatabase 类中是private。你不需要自己打电话给open_connection()。应该是:public function test(){ $this-&gt;db-&gt;query( "whatever"); }。除非你不能保留这个函数static,因为你需要实例化这个类(所以调用了构造函数)。
    • 真的很感谢你的帮助,写错了关于 ds 的错误,无论如何,你让我很清楚,我们不能用那个静态的 OBJ,所以我需要实时实例化我的 OBJ使其工作并在非静态的情况下使用它。真的很有帮助,再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多