是否可以在 Codeigniter 中动态扩展模型?

发布于 10-17 01:00 字数 436 浏览 6 评论 0原文

我在 CI 中的模型经过设置,以便在需要某些功能时加载“子”模型。为了使我的代码尽可能易于访问和干净,我希望这些子模型能够扩展它们被调用的模型。

因此,如果我有两个模型:

<?php

class Mymodel extends Model
{



}

并且:

<?php

class Submodel extends Model
{

    function test() { do something.. }

}

然后我需要以某种方式让子模型扩展 mymodel,以便我可以执行类似 $this->mymodel->test() >。子模型扩展的不一定是 mymodel,它可以是任何模型。有什么想法吗?

感谢您抽出时间。

my models in CI are set up so that they load "sub"-models whenever they need certain functions. In order to keep my code as accessible and clean as possible, I want those submodels to extend the model they are called to.

So if I have two models:

<?php

class Mymodel extends Model
{



}

And:

<?php

class Submodel extends Model
{

    function test() { do something.. }

}

Then I need to, somehow, be able get the submodel to extend mymodel, so that I can do something like $this->mymodel->test(). It doesn't have to be mymodel that submodel extends, it could be any model. Any ideas?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

姜生凉生2024-10-24 01:00:59

您对类之间的继承的理解不正确。

继承只能以一种方式进行,即向下。

如果 Myodel extends Submodel 你的 $this->mymodel->test() 可以工作,但它没有意义,因为子(子)对象应该继承来自父对象,而不是相反。

打个比方,你不会看着一个孩子并告诉父母,“你看起来就像你的孩子”,因为孩子是父母的一部分代表。

您需要从字面上理解“扩展”这个词,您实际上是在“扩展”父级的功能。

===================

我相信您可以实现此目的的一种方法是创建幽灵函数,该函数只需加载正确的模型并调用该模型函数(尽管我不建议这样做) ,调试可能会变得非常混乱

因为根据您的示例

<?php

class Mymodel extends Model
{

    function test() {
        $this->load->model('submodel');
        $this->submodel->test();
    }

}

子模型

<?php

class Submodel extends Model
{

    function test() { do something.. }

}

但是,如果您想要干净的代码,这不是正确的方法,请尝试观察继承,并在设计数据时牢记这一点。< /强>

You have an incorrect understanding of inheritance between classes.

Inheritance only flows one way, Down.

if Myodel extends Submodel your $this->mymodel->test() would work, but it does not make sense as sub (child) objects are suppose to inherit from parent objects, not the other way around.

As an analogy, you wouldn't look at a child and tell the parent, "You look just like your child", because it is the child that is a part representation of the parent.

you need to take the word extends very literally, you are literally 'extending' the functionality of the parent.

===================

One way i believe you could accomplish this is to create ghost functions that simply load the proper model and call that models function (though I do not recommend this as it could get very confusing for debugging.

per your example

<?php

class Mymodel extends Model
{

    function test() {
        $this->load->model('submodel');
        $this->submodel->test();
    }

}

Submodel

<?php

class Submodel extends Model
{

    function test() { do something.. }

}

BUT again, if you are going for clean code, this is NOT the way to go, try and observe inheritance, and design your data with that in mind.

鹊巢2024-10-24 01:00:59

您可以创建可以扩展 codeigniter 模型的实用程序模型,然后您的所有模型都可以扩展该实用程序模型。您要添加到实用程序模型中的方法将可供其所有子类(即您的模型)使用。

如何调用您的类没有或没有从任何其他类继承的方法?查看您的代码,Mymodel 和 Submodel 之间的类中没有关系。

You can create utility model which may extends codeigniter's model and then all your models can extend that utility model. The methods you are going to add to the utility model will be available to all it's child classes aka your models.

How can you call a method which your class does not have or does not inherit from any other classes? Looking at your code, there is no relationship in your classes between Mymodel and Submodel.

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