Kohana 3模块结构问题
大家!我有一个关于 Kohana 3 的新问题,或者更确切地说是关于模块结构的新问题。我开发了一个名为 Textblock 的小模块。它是关于普通页面或站点布局的小插入(例如问候语或口号、公司名称)。它包含控制器和模型。模型继承 Sprig_MPTT。我想要实现的一个功能是可以像这样调用这个模块:
$textblock = Textblock::get_single(1); //by id
$children = Textblock::get_children_of(4); //id of parent
而不是
$textblock = Sprig::factory('Textblock')->get_single(1);
$children = Sprig::factory('Textblock')->get_children_of(4);
这些方法在 Model_Textblock 类中定义为 static
。
因此,我创建了一个包装类 Textblock
,它继承 Model_Textblock
。例如,如果我突然想将 Sprig 更改为 Jelly,该怎么办?前景根本不会改变。恕我直言,另一个优点是对于任何想要使用此模块的人来说都更加清晰(例如,它可能是团队中的另一个程序员)。
但有人怀疑我是否走错了路......所以,问题本身:建议的是组织我的模块的正确方法吗?或者最好在需要 Textblock 功能的地方保留普通的 Sprig::factory('Textblock')
,删除额外的包装类并删除 static
?
everybody! I have a new question about Kohana 3, or rather about a module structure. I develop a small module called Textblock. It's about an ordinary page or a small insertion to the site layout (e.g. a greeting or a slogan, company name). It contains both controllers and models. Models inherit Sprig_MPTT. And one feature I'd like to implement is one could be able to call this module like this:
$textblock = Textblock::get_single(1); //by id
$children = Textblock::get_children_of(4); //id of parent
and not
$textblock = Sprig::factory('Textblock')->get_single(1);
$children = Sprig::factory('Textblock')->get_children_of(4);
Those methods are defined in Model_Textblock class as static
.
So, I made a wrapper class Textblock
, that inherits Model_Textblock
. What if I suddenly want change Sprig to Jelly, for example? Foreground won't change at all. Another advantage, imho, is more clarity for anyone, who wants to use this module (e.g. it could be another programmer in the team).
But there's a doubt if I'm on a wrong way... So, the question itself: is the suggested a right way to organize my module? Or it's preferable to keep ordinary Sprig::factory('Textblock')
where Textblock's functionality is needed, remove additional wrapper class and remove static
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需扩展
Model_Textblock
。您可以创建一个模型实例并调用其方法:但是这样您应该在静态类中复制模型方法(而不是 DRY)。另外,如果您有多个模型怎么办?你想要的(据我所知)就是轻松更改 AR 驱动程序。所以我更喜欢这种类:
并像
Textblock::model('textblock')->get_single($id)
一样使用它。There is no need to extend
Model_Textblock
. You can create a model instance and call its method:But this way you should copy model methods in your static class (not DRY). Also, what if you have more than one model? All you want (as I understand) is to easily change AR driver. So I'd preffer this kind of class:
and use it like
Textblock::model('textblock')->get_single($id)
.