2013-02-15 75 views
1

這是我的代碼:簡單的ORM結構笨

<?php 

class VortexORM { 

    private static $orm = null; 

    public function __get($name) { 
     return $this->$name; 
    } 

    public function __set($name, $value) { 
     $this->$name = $value; 
    } 

    public static function getInstance() { 
     if (VortexORM::$orm == null) 
      VortexORM::$orm = new VortexORM(); 
     return VortexORM::$orm; 
    } 

    public static function set($name, $value) { 
     $orm = VortexORM::getInstance(); 
     //echo "Setting [ <b>{$name}</b> :: <i>{$value}</i>]"; 
     $orm->$name = $value; 
    } 

    public static function get($name) { 
     $orm = VortexORM::getInstance(); 
     // echo "Getting [ <b>{$name}</b> :: <i>{$orm->$name}</i>]"; 
     return $orm->$name; 
    } 

} 

獲取數據我用:

var_dump(VortexORM::get('admin_links')); 
var_dump(VortexORM::get('admin')); 

設定數據使用:

VortexORM::set('admin_links',array(....)); 

但是,我得到的以下警告:

A PHP Error was encountered 
Severity: Notice 
Message: Undefined property: VortexORM::$admin_links 
Filename: Vortex/VortexORM.php 
Line Number: 8 
NULL 
A PHP Error was encountered 
Severity: Notice 
Message: Undefined property: VortexORM::$admin 
Filename: Vortex/VortexORM.php 
Line Number: 8 

爲什麼我會收到這些警告?

我希望能夠訪問它像這樣笨的靜態函數:

$this->vortexorm->admin_links = array(....); 

回答

-1

OK,我只是測試這個代碼:

<?php 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

class VortexORM { 

    private static $orm = null; 

    public function __get($name) { 
     return $this->$name; 
    } 

    public function __set($name, $value) { 
     $this->$name = $value; 
    } 

    public static function getInstance() { 
     if (VortexORM::$orm == null) 
      VortexORM::$orm = new VortexORM(); 
     return VortexORM::$orm; 
    } 

    public static function set($name, $value) { 
     $orm = VortexORM::getInstance(); 
     //echo "Setting [ <b>{$name}</b> :: <i>{$value}</i>]"; 
     $orm->$name = $value; 
    } 

    public static function get($name) { 
     $orm = VortexORM::getInstance(); 
     // echo "Getting [ <b>{$name}</b> :: <i>{$orm->$name}</i>]"; 
     return $orm->$name; 
    } 

} 

VortexORM::set('admin_links',"test"); 

var_dump(VortexORM::get('admin_links')); 

這只是工作對我很好。給出以下輸出:

string(4) "test" 

所以,我想,你可能只是試圖檢索其屬性之前?或者請分享更多細節。另外,你爲什麼試圖創建新的ORM,我們可以輕鬆地在use doctrine with codeigniter