简体   繁体   English

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

[英]Is it possible to dynamically extend a model in Codeigniter?

my models in CI are set up so that they load "sub"-models whenever they need certain functions. 设置CI中的模型,以便它们在需要某些功能时加载“子”模型。 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() . 然后,我需要以某种方式能够使子模型扩展mymodel,以便可以执行类似$this->mymodel->test() It doesn't have to be mymodel that submodel extends, it could be any model. 子模型扩展不必是mymodel,也可以是任何模型。 Any ideas? 有任何想法吗?

Thanks for your time. 谢谢你的时间。

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. 如果Myodel extends Submodel你的$this->mymodel->test()会起作用,但它没有意义,因为子(子)对象假设从父对象继承,而不是相反。

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. 你需要extends字面上理解这个词,你实际上是“扩展”父母的功能。

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

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. 我相信你可以做到这一点的一种方法是创建ghost函数,只需加载正确的模型并调用该模型函数(虽然我不建议这样做,因为它可能会让调试变得非常混乱。

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. 但是,如果要使用干净的代码,那不是走的路,尝试观察继承,并牢记这一点来设计数据。

You can create utility model which may extends codeigniter's model and then all your models can extend that utility model. 您可以创建可以扩展codeigniter模型的实用程序模型,然后所有模型都可以扩展该实用程序模型。 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. 查看您的代码,Mymodel和Submodel之间的类没有关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM