2013-08-20 67 views
0

我在index.php中創建了一個serviceFactory的實例,它只是自己獲取/存儲(私人)變量。但是,由於某種原因,__get和__set魔術方法不起作用。在索引中執行var_dump($ serviceFactory-> language)將返回false。當我創建serviceFactory的實例時,我將$ router實例傳遞給$ serviceFactory構造函數。路由器正在工作(否則它不會顯示控制器或視圖),但出於某種原因,當我嘗試從$ serviceFactory實例中__get語言時,它返回false,我對此感到困惑。PHP魔術方法 - __get和__set不工作在PHP MVC框架服務工廠

的index.php

//create an instance of serviceFactory 
$serviceFactory = new serviceFactory($router); 
var_dump($serviceFactory->language); 

服務工廠

class serviceFactory { 
    private $data = array(); 
    public function __construct($router) { //store the router data 
     $data['language'] = $router->getKey('language'); 
     $data['class'] = $router->getKey('className'); 
     $data['method'] = $router->getKey('method'); 
     $data['arg'] = $router->getKey('arg'); 
    } 
    public function __set($key, $val) { 
     $this->data[$key] = $val; 
    } 
    public function __get($key) { 
     if(isset($this->data[$key])) { 
      return $this->data[$key]; 
     } 
     else { 
      return false; 
     } 
    } 
} 
+1

您已經通過他們相同的實例。 – vascowhite

+0

@vascowhite在baseView $ this-> language =「」所以有些東西沒有工作。有任何建議嗎? – Aaron

+0

@vascowhite查看編輯我的問題 – Aaron

回答

0

簡單,你犯了一個常見的錯誤與實例工作:

$data['language'] = $router->getKey('language'); 
    $data['class'] = $router->getKey('className'); 
    $data['method'] = $router->getKey('method'); 
    $data['arg'] = $router->getKey('arg'); 

都應該

$this->data['language'] = $router->getKey('language'); 
    $this->data['class'] = $router->getKey('className'); 
    $this->data['method'] = $router->getKey('method'); 
    $this->data['arg'] = $router->getKey('arg'); 
+0

非常感謝你!我注意到,昨天晚上,它現在工作正常。在構建我的框架的下一個階段 - >登錄系統:/ – Aaron