![]() |
how to call a function of another class - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: how to call a function of another class (/showthread.php?tid=34542) Pages:
1
2
|
how to call a function of another class - El Forum - 10-02-2010 [eluser]Sumon[/eluser] here are two simple classes Code: class A extends Controller { how could i call FB1() from FA1() ? Thanks in advance. how to call a function of another class - El Forum - 10-02-2010 [eluser]echoDreamz[/eluser] Rethink your design. Controller methods are created and assigned for that specific controller. If you need to share them between controllers (I have methods I use on many different controllers) extend it twice like so... (or you can use libraries / helpers). Code: class SharedController extends Controller how to call a function of another class - El Forum - 10-02-2010 [eluser]dstegelman[/eluser] Why are you calling a function from a controller inside of a controller. I can't think of many situations where you need to do this. if you need commonly shared data, you should probably be using a model to get it instead. how to call a function of another class - El Forum - 10-02-2010 [eluser]echoDreamz[/eluser] I use it simply because models are for database / low level stuff. My "middle man" controller is for things like javascript, css, other HTML stuff, creating templates / sections etc. EDIT: However I am not calling another controller... I am simply calling methods from within the controller I extended. how to call a function of another class - El Forum - 10-02-2010 [eluser]Dennis Rasmussen[/eluser] Listen to the guys giving you help. If you need to run some code in more than one controller then write the code in a library or even a model for reuse. You don't call methods from another controller, that kinda makes no sense. how to call a function of another class - El Forum - 10-03-2010 [eluser]Sumon[/eluser] [quote author="echoDreamz" date="1286075352"] Code: class SharedController extends Controller Fatal error: Class 'SharedController' not found in D:\xampplite\htdocs\shopno-dinga-dsebd\application\controllers\sharedController.php on line 3 Please note that, sharedcontroller and topcontrollers are 2 separate files how to call a function of another class - El Forum - 10-03-2010 [eluser]echoDreamz[/eluser] This is because CI will only load 1 controller. Not both. So you need to manually include the file that houses shared controller. I went around this by using the PHP5 autoload method. Code: /** This allows me to use libraries that do not follow the CI scheme, or have multiple levels of controllers. how to call a function of another class - El Forum - 10-03-2010 [eluser]InsiteFX[/eluser] This is what a MY_Controller is for! you should really listen to what users explain to you in the forums. InsiteFX how to call a function of another class - El Forum - 10-03-2010 [eluser]echoDreamz[/eluser] [quote author="InsiteFX" date="1286186780"]This is what a MY_Controller is for! you should really listen to what users explain to you in the forums. InsiteFX[/quote] MY_Controller only works when you have your prefix set to MY_. We have different prefixes for our files vs customer specific files libraries / helpers. So for instance you have the prefix set to Customer_ and you try and use MY_Controller it will not work and will error saying class not found. This is why I use the autoloader above. But yes, for those who use single prefixed libraries etc, it will work fine. [quote author="Sumon" date="1286185390"][quote author="echoDreamz" date="1286075352"] Code: class MY_SharedController extends Controller Fatal error: Class 'SharedController' not found in D:\xampplite\htdocs\shopno-dinga-dsebd\application\controllers\sharedController.php on line 3 Please note that, sharedcontroller and topcontrollers are 2 separate files[/quote] The edited code above will work. how to call a function of another class - El Forum - 10-03-2010 [eluser]InsiteFX[/eluser] You can change the MY_ prefix to what ever you like! It doe's not have to be MY_ InsiteFX |