How to create reusable model or controller ? |
[eluser]Wades[/eluser]
Hi guys, after using CI for developing several websites, I really love this framework. and now I'd like to make it more flexible, so I want to make some reusable models or controller. What I want to do is that: suppose there are two models: A_model and B_model then there is a function in A_model called function_a(); so how could I use function_a() of A_model in B_model? Is anybody can help me?
[eluser]Référencement Google[/eluser]
Why not that B extends the A model? Not quiet sure we can extend models with others but give it a try.
[eluser]imzyos[/eluser]
extends... mmm, all Models must extends CI_Model, helper, or your own library.
[eluser]Wades[/eluser]
thanks elitemedia, I've tried this way, yes, B can extends A model, but if there is another base model C, then B also wants to use the functions in C, then is there any way to get this work? I want to make it something like java, when you want to use a class, then just import it and then it can be used. anyone who has this kind of experience in CI? thank you!
[eluser]Luci3n[/eluser]
PHP5 can only extend one class, you will need to create a generic class which extends the the base model class then after extend the generic class See this thread
[eluser]xwero[/eluser]
A workaround solution is to create a library where you define functions you want to share. Code: class modelfunctions{ I like this way better than extending models because you have more loose coupling between the models.
[eluser]Wades[/eluser]
wow, tks a million xwero, this is really an great idea
[eluser]Référencement Google[/eluser]
xwero this is very smart! Thanks for sharing this idea.
[eluser]xwero[/eluser]
no problem i get many ideas from looking at other ones solutions so sharing comes natural. |
Welcome Guest, Not a member yet? Register Sign In |