dongpanbo4727 2014-01-15 01:18
浏览 24

将模型加载到$ this范围内

I'm doing a learning exercise where I'm building a pretty basic MVC framework. I'm really only doing this to learn more about OOP and it's pros, cons and common pitfalls.

I'm trying to replicate a behaviour, or syntax might be more correct, similar to the very popular framework codeigniter.

That is, I want to manually be able to load a Model from inside my Controller.

Here's how I want to perform it, and subsequently use it.

    $this->load->model("mymodel");
    $this->mymodel->some_function();

I have my loader working, it tries to run the class and this is how the load->model code looks like

public function model($model)
{
    if(file_exists(APPLICATION.'models/'.$model.'.php'))
    {
        include(APPLICATION.'models/'.$model.'.php');
        $this->{$model} = new $model;
    }
}

The problem I am having is I get a error running this code saying that the class $model (this should be transformed into mymodel in this case) doesn't exist.

How do I make it so that $model translates into mymodel so the code would perform a action as such: new mymodel;

Thanks for any help, I'm quite the novice in OOP so I might have gotten confused here but really cannot seem to figure this out :/

$this->{$model} does, however, translate into $this->mymodel.

Instead of creating a new thread, I'll add to this.

My next issue has already arisen, since it's basically a followup problem I'll add it here too.

$this->mymodel->some_function() returns the following error;
Notice: Undefined property: Home::$mymodel in C:\xampp\htdocs\application\controllers\home.php on line 16

This error shows when running $this->mymodel->some_function();

Home is the loaded controller.

Hultin

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大