我不能创建类的新实例


yii cant create new instance of class

在我看来,我想做的是:

$accountLogin = new AccountLogin;

,它应该创建一个在我的供应商库中定义的类的新实例。

class AccountLogin extends CFormModel
{
..
}

但是我得到了unable to open stream('AccountLogin')

我做错了什么?我是否应该在某个地方指定目录所在的位置?

你必须先导入你的类。

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

类:

class RegionSingleton extends CApplicationComponent
{
    private $_model=null;

    public function setModel($id)
    {
        $this->_model=Region::model()->findByPk($id);
    }
    public function getModel()
    {
        if (!$this->_model)
        {
            if (isset($_GET['region']))
                $this->_model=Region::model()->findByAttributes(array('url_name'=> $_GET['region']));
            else
                $this->_model=Region::model()->find();
        }
        return $this->_model;
    }
    public function getId()
    {
        return $this->model->id;
    }
    public function getName()
    {
        return $this->model->name;
    }
}

并将这个类包含在config main中,然后您可以在所有应用程序中快速调用它:

'components'=>array(
        'region'=>array('class'=>'RegionSingleton'),
         ...
        )

现在,我们可以这样命名它:

Yii::app()->region->model;

for have the model, or also

Yii::app()->region->id

用于检索id。

我们还可以使用

设置模型
Yii::app()->region->setModel($id)

参考:link1