Kohana 3 中空抽象类的用途是什么?
我在实践中没有太多使用抽象类,尽管我了解它们是什么:一种指示子类必须实现哪些方法的方法。
我刚刚通过 Doxygen 放置了一个 Kohana 项目,可以看到完整的类层次结构。我看到,在链的顶部我们有一个工厂:
abstract class Kohana_Model {
public static function factory($name){
// Add the model prefix
$class = 'Model_'.$name;
return new $class;
}
}
直接继承在它下面,我们有一个空的抽象类:
abstract class Model extends Kohana_Model {}
...在它下面,有三个继承的类:Kohana_Model_Database
,<代码>Kohana_ORM和Model_Foobar
。
有人可以解释一下编程的原因吗?在链中这么高的位置有一个空的抽象类的目的是什么? (而且,根本?)
当 Model 和 Kohana_Model 之间没有(显然?)没有其他分支或继承时,为什么不让 Kohana_Model_Database、Kohana_ORM 和 Model_Foobar 直接从 Kohana_Model 继承呢?
I haven't used abstract classes much in practice, though I understand what they are : a way to dictate to subclasses which methods must be implemented.
I just put a Kohana project through Doxygen and can see the class hierarchy in its entirety. I see that, at the top of the chain we have a factory:
abstract class Kohana_Model {
public static function factory($name){
// Add the model prefix
$class = 'Model_'.$name;
return new $class;
}
}
Inherited directly below that, we have an empty abstract class:
abstract class Model extends Kohana_Model {}
... And below that, there are three inherited classes: Kohana_Model_Database
, Kohana_ORM
, and Model_Foobar
.
Would someone please explain the programming reasoning for this - what is the purpose of having an empty abstract class this high up in the chain? (and, at all?)
Why not have Kohana_Model_Database, Kohana_ORM, and Model_Foobar inherit directly from Kohana_Model, when there is (apparently?) no other branching or inheritance going on between Model and Kohana_Model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的答案是级联文件系统和透明扩展。
Answers you're seeking for are Cascading File System and Transparent Extensions.
它允许您通过默认调用来创建模型
,然后它将自动扩展
Kohana_Model
,一切都会很顺利。它还允许您通过创建自己的模型文件
(覆盖抽象模型类)来扩展 Kohana_Model,并允许您添加自定义功能。然后,当您将 Kohana 版本升级到(例如)3.4 时,您的扩展模型不会被新的 Kohana 文件覆盖。
It allows you to create a model by calling
by default, and that will automatically then extend
Kohana_Model
and things will be hunky dory.It also lets you extend Kohana_Model by creating your own Model file
which overrides the abstract Model class, and allows you to add custom functionality. Then, when you upgrade your Kohana version to (say) 3.4, your extended Model doesn't get overwritten by the new Kohana files.