For those who thinking of - Class child extends parent/Class extend or inheritance |
[eluser]Unknown[/eluser]
It's Christmas Break for me, so I decided to take some time to mess with OOP (Object-Oriented Programming) with CodeIgniter. This post is solely dedicated for those who are NEW to CodeIgniter and planning to do any OOP in CodeIgniter. If you are planning to use OOP in Code Igniter Framework, find another one. But, if you want a framework that is simple and straightforward, Code Igniter is the winner. I will demonstrate how to use Class extends/inheritance successfully in CI, though it is not the best; Let's take a dive! Stuff that did not work for me: METHOD A Code: class Love extends Controller{ *If you want this to work, but can simply cut the "$this->load->library('table');" and paste it to the Child Construct, and make sure the table library is not globally enable. IMO, it's not practical. *---IF anyone can point out a way to get this method working OR even an explanation pertaining to this scenario, i really do appreciate it ![]() Stuff that worked!! :- METHOD B Create love.php in the same folder, which in this case is Controllers. Code: class Love extends Controller{ Create Hate.php in the same folder as love.php Code: require_once(APPPATH.'controllers/love.php'); TADA, just call it via index.php/hate/call , it will work like a charm. BUT, this method is sorta NOT the "proper" way of coding for MVC. THE OFFICIAL/Recommended WAY(IMO, it looks like a workaround to me): METHOD C Create a FILE MY_controller.php in application/libraries, EXACT NAME and CAPITAL LETTER, otherwise it will not work. I tried MY_Love, it just doesn't load. Code: //Toss all your parent classes here Code: class Hate extends MY_Love{ It will work just fine ![]() BUT, my question is why the METHOD A does not work, as METHOD B works?? The only different is that B child class is ran on separate php file. Any comments or explanations will be appreciated!!
[eluser]bl00dshooter[/eluser]
First: Each class is like a controller. You're not suposed to have 2 controllers on the same file, so it doesn't work. There is a reason the filename must be equal to the class name, it's how codeigniter routing works. Second: Phil Sturgeon already explained how to do what you want in his blog: http://philsturgeon.co.uk/index.php/news...ing-it-DRY Third: How's codeigniter not playing well with OOP? It's based on OOP, controllers and models are OOP, etc. I think you incorrectly explained yourself or you don't understand how things work. Fourth: Merry Christmas, sskl.
[eluser]denver001[/eluser]
<a href="http://motorhomescampervans.net" rel="nofollow">Motorhome Sales</a> |
Welcome Guest, Not a member yet? Register Sign In |