正在创建用户对象


Creating User Object

我正在开发一个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();  
    }
}

$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';
    }
}