真实对象和MVC [英] Real objects and MVC

查看:128
本文介绍了真实对象和MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到那里,我总是这样编码对象:

Until there, I always coded object this way :

// initialization
$husband = new User('Bob');
$wife = new User('Sarah');

// action
$husband->dance();
$wife->read();

// get
echo "The husband is ".$husband->getAge()." years old";

但是使用CodeIgniter(和MVC),看起来最好这样想:

But with CodeIgniter (and MVC), it seems it's better to think this way :

// load model
$this->load->model('user');

// action
$this->user->dance('Bob');
$this->user->read('Sarah');

// get
echo $this->user->getAge('Bob');

但在这种情况下,如何处理真实对象?例如对象Bob和对象Sarah?
也许我缺少的东西,但似乎对我来说Model(第二个例子)!= Object(第一个例子)。
这个对象的概念是不兼容的吗?

But in this case, how to deal with "real objects" ? For example the object "Bob" and the object "Sarah" ? Maybe i'm missing something but it seems for me that Model (second example) != Object (first example). Do this conception of objects are incompatibles ?

我有目录视图,目录控制器和目录模型。是否还应该有一个objects目录?

I have the directory view, the directory controller, and the directory model. Should I have also a "objects" directory ?

推荐答案

如果你还想使用CI, p>

If you still want to use CI, you can do this:

// load the class. I think there was a second parameter to avoid instantiation, not sure though. Either way, this will at least load the class
$this->load->model('user',false);

$husband = new User('Bob');
$wife = new User('Sarah');

// action
$husband->dance();
$wife->read();

这仍然是MVC。丑陋的类加载部分是因为CI没有在引用类自动加载,所以你必须手动进行。也许您可以整合通用类装载器不知何故。

This is still MVC. The ugly class loading part is there because CI does not have class autoloading on referenciation, so you have to do it manually. Maybe you can integrate a Universal Class Loader somehow.

这篇关于真实对象和MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆