CodeIgniter Forums
Probably a silly question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Probably a silly question (/showthread.php?tid=30467)



Probably a silly question - El Forum - 05-16-2010

[eluser]theprodigy[/eluser]
I'm just a little curious here, but:
When you extend the majority of CodeIgniter's core libraries, it just works. You still reference it the same way.

For instance, when you create a MY_Router to extend the functionality of CI_Router, you still only reference it with
Code:
$this->router
You don't have to use
Code:
$this->MY_router

Why is it then, that when building controllers and models, you have to use MY_ in the extension (If extending MY_Controller or MY_Model). Is it because of the syntax of "extends" and it not being a class instantiation?


Probably a silly question - El Forum - 05-16-2010

[eluser]WanWizard[/eluser]
When you look in the loader code, you'll see that first the orginal class is loaded ('Class CI_Router'), then a check is made for the existence of an extension ('Class MY_Router extends CI_Router'), which is then loaded.

Based on the existence of the extension, it then does a '$CI->Router = new CI_Router()' or a '$CI->Router = new MY_Router()', thus making sure you can access the router class consistently, whether or not an extension is present.


Probably a silly question - El Forum - 05-16-2010

[eluser]theprodigy[/eluser]
yeah, not exactly sure why I asked this last night. I think my mind went to bed before I did. lol

It's basically a simple case of object vs class. When writing a controller, you are extending a class. With the other scenario ($this->router), you are using an object. CI could have just as easily named it $this->get_me_my_brown_pants, if they wanted to, and it would still work, because it's just a variable name that holds an instance of a class, whether that class be CI_Router or MY_Router.

P.S. Thanks for your reply, anyway. ;-)


Probably a silly question - El Forum - 05-16-2010

[eluser]WanWizard[/eluser]
LOL

If you want to, you still can:
Code:
$this->load->library('Library', array(), 'get_me_my_brown_pants');