![]() |
Calling library method inside model error (with solution) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: Calling library method inside model error (with solution) (/showthread.php?tid=71810) |
Calling library method inside model error (with solution) - elche - 09-26-2018 Hi everyone, I was spent a lot of time solving this situation in a my current project and I would like to share with all of you this solution to whom may help. It's a matter of calling a class method from model method called from a constructor without class being completely loaded. Probably Too trivial.. and for beginners but.. we do not always have a good day ![]() Suppose we have this case: (I apologize about wrong code, capitals, etc.. I have rewritten this code without testing it, but making it similar and removing useless information from my current project) Controller mycontroller ------------------------- Code: class mycontroller extends CI_Controller() { library mylibrary ----------------- Code: protected $obj; library mysecondelibrary ------------------------- Code: class Mysecondlibrary{ model mymodel ----------------- Code: class Mymodel extends CI_Model { If we call a library method from model method which it was called from a library constructor class this will crash. We need to call method after libraries are loaded. To solve this, we need to call a class init method after loading. library mylibrary ----------------- Code: protected $obj; library mysecondelibrary ------------------------- Code: class Mysecondlibrary{ RE: Calling library method inside model error (with solution) - Wouter60 - 09-26-2018 Whats the reason why you are loading the second library into the first one? Wouldn't it be better to: PHP Code: class Mysecondlibrary extends Mylibrary That way, you can call methods in Mylibrary through $this->mysecondlibrary-> RE: Calling library method inside model error (with solution) - elche - 09-26-2018 (09-26-2018, 01:15 PM)Wouter60 Wrote: Whats the reason why you are loading the second library into the first one? Wouldn't it be better to: Thanks for your contribution, in this case I am using mylibrary only to load all libraries, methods, helpers into the memory but mylibrary has no methods or variables that I can use from mysecondlibrary. RE: Calling library method inside model error (with solution) - Gurutechnolabs - 10-01-2018 Hello, you can call methods in My library through $this->mysecondlibrary-> class Mysecondlibrary extends My library <?php class Blog extends CI_Controller { public function index() { echo 'Hello World!'; } } |