Kohana 3 中空抽象类的用途是什么?

发布于 2025-01-03 12:43:25 字数 714 浏览 2 评论 0原文

我在实践中没有太多使用抽象类,尽管我了解它们是什么:一种指示子类必须实现哪些方法的方法。

我刚刚通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2025-01-10 12:43:25

您正在寻找的答案是级联文件系统透明扩展

Answers you're seeking for are Cascading File System and Transparent Extensions.

旧故 2025-01-10 12:43:25

它允许您通过默认调用来创建模型

class News_Model extends Model

,然后它将自动扩展 Kohana_Model ,一切都会很顺利。

它还允许您通过创建自己的模型文件

class Model extends Kohana_Model

(覆盖抽象模型类)来扩展 Kohana_Model,并允许您添加自定义功能。然后,当您将 Kohana 版本升级到(例如)3.4 时,您的扩展模型不会被新的 Kohana 文件覆盖。

It allows you to create a model by calling

class News_Model extends Model

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

class Model extends Kohana_Model

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文