2013-02-04 46 views
0

我正在開發一個codeigniter應用程序,並希望在我的應用程序中創建一個用戶對象,以便用於測試。創建用戶對象

以下代碼在後端控制器中運行,我不確定是否應該這樣做。

class Backend_Controller extends MY_Controller 
{ 
    public $current_user = new stdClass; 
    public $current_user->group = 'User Group'; 
    public $current_user->name = 'Kevin Smith'; 

    public function __construct() 
    { 
     parent::__construct(); 

    } 
} 

回答

2

$current_user->group不是變量聲明。你只是分配一個已經聲明的變量的屬性。

此外,您不能在類聲明中進行函數調用,您只能設置常量。

PHP文件:http://www.php.net/manual/en/language.oop5.properties.php

您需要使用的構造使對象。

class Backend_Controller extends MY_Controller 
{ 
    public $current_user; 

    public function __construct() 
    { 
     parent::__construct(); 

     $this->current_user = new stdClass; 
     $this->current_user->group = 'User Group'; 
     $this->current_user->name = 'Kevin Smith'; 

    } 
} 
+0

謝謝你Rocket。 –

+0

不客氣! :-) –