PHP Magic方法无法使用扩展类

时间:2015-03-10 16:42:39

标签: php abstract-class magic-methods

我有问题。如图所示,我已经制作了一个抽象类,其中包含了在我的类中使用的魔术方法。当我尝试使用类Pratica(StandardClass的扩展)并尝试访问一个简单的属性时,PHP返回此错误:尝试在/var/www/property/htdocs/pratiche/dettaglio_pratica.inc中获取非对象的属性第40行的.php。 问题是在我的本地服务器中它可以工作,但在公司服务器中却没有。是配置问题?或者是代码问题?公司服务器使用PHP 5.3.3,在语言环境中我使用PHP 5.3.6。

任何人都可以帮助我吗?非常感谢。 费德里科

以下是代码的提取:

    abstract class standardClass {

        protected $data = array();
        public function __construct() {

        }
        public function __set($property, $value) {

            $this->data[$property] = $value;

        }
        public function __get($property) {

            if (isset($this->data[$property]) === true && array_key_exists($property, $this->data)) {

                return $this->data[$property];

            }

        }
        public function __unset($property) {

            unset($this->data[$property]);

        }

        public function __isset($property) {

            return isset($this->data[$property]);
        }

    }

    class Pratica extends StandardClass {

        const _NOME_SESSIONE_PRATICA_   = "pratica";

        public function __construct($id_pratica) {

            $this->load($id_pratica);

        }
        public static function init($id_pratica = null) {

            Session::init();

            Session::sessionStart();    

            if (Session::existVar(self::_NOME_SESSIONE_PRATICA_) == true) {
                if (is_string(Session::getVar(self::_NOME_SESSIONE_PRATICA_)) == false) {
                    return new Pratica($id_pratica);
                }
                else {
                    return unserialize(Session::getVar(self::_NOME_SESSIONE_PRATICA_));
                }

            }

            return new Pratica($id_pratica);

        }

        private function load($id) {

            global $db;


            $sql = <<<EOQ
                    SELECT *
                      FROM elenco_pratiche
                     WHERE id = $id;
    EOQ;

            $result = $db->sql($sql);

            if ($db->righe() > 0) {
                $riga = $db->riga($result);

                $this->modify($riga);
            }
        }

        public function modify($pratica) {

            foreach ($pratica as $key => $value) {
                $this->data[$key] = $value;
            }
        }

        public function __destruct() {

            Session::addVar(self::_NOME_SESSIONE_PRATICA_, serialize($this));

        }
    }

class Session {

    protected $log;
    static public $is_started = false;

    static private $auto_start = false;

    static private $session_save_path = null;
    static private $session_id = null;
    static private $session_name = null;

    static private $cookie__lifetime = null;
    static private $cookie_path = null;
    static private $use_cookies = false;
    static private $use_only_cookies = false;
    static public function init() {

        if (ini_get("session.auto_start") == 1) {
            self::$auto_start = true;
            self::$is_started = true;
        }
        elseif (session_id() <> "") {
            self::$is_started = true;
        }

        self::$session_save_path = ini_get("session.save_path");

        if (ini_get("session.use_cookies") == 1) {
            self::$use_cookies = true;
        }

        if (ini_get("session.use_only_cookies") == 1) {
            self::$use_only_cookies = true;
        }


    }
    static public function isSessionAutostart() {

        return self::$auto_start;

    }
    static public function isStarted() {

        return self::$is_started;

    }
    static public function sessionStart() {

        if (self::$auto_start == false && self::$is_started == false) {

            session_start();

            self::$session_id = session_id();
            self::$session_name = session_name();
            self::$is_started = true;

        }

    }
    static public function setSessionId($id) {

            session_id($id);
            self::$session_id = session_id();

    }
    static public function getSessionId() {

            return(session_id());

    }
    static public function addVar ($name,$value) {

        if (self::$is_started == true) {
            $_SESSION[$name] = $value;
        }
    }
    static public function getVar ($name) {

        if (self::$is_started == true) {
            if (isset($_SESSION[$name]) == true) {
                return $_SESSION[$name];
            }
            else {
                return false;
            }

        }
    }
    static public function deleteVar($name) {

        if (isset($_SESSION[$name]) == true) {
            unset($_SESSION[$name]);
        }
    }

    static public function existVar($name) {

        $x = isset($_SESSION[$name]);

        return isset($_SESSION[$name]);

    }
    static public function dump($id = null) {

        $log = new log();

        if (is_null($id) == true) {
            foreach ($_SESSION as $key => $value) {
                $log->user(basename(__FILE__) . "_SESSION[" . $key . "] = " . $value,"federico");       
            }
        }
        else {
                $log->user(basename(__FILE__) . "_SESSION[" . $id . "] = " . $_SESSION[$id],"federico");        
        }

    }
    static public function closeSession() {

        if (self::$is_started == true) {

            session_unset();
            session_destroy();

            self::$is_started = false;

        }
    }
}

$pratica = Pratica::init($id);

$id_pratica = $pratica->id;

0 个答案:

没有答案