CodeIgniter Forums
load a Class that extends a 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: load a Class that extends a Class ? (/showthread.php?tid=16339)



load a Class that extends a Class ? - El Forum - 03-03-2009

[eluser]junkwax[/eluser]
How can I load a class that extends a class ?

I have:

application/libraries/Animal.php
application/libraries/Cat.php

Animal.php contains

Class Animal {

}

Cat.php contains

Class Cat extends Animal
{


}


I do $this->load->library( 'cat' );

but it doesnt work.

If I remove "extends Animal" it works.


load a Class that extends a Class ? - El Forum - 03-03-2009

[eluser]pistolPete[/eluser]
There are two solutions:

1) Load both classes in this order:
Code:
$this->load->library('animal');
$this->load->library('cat');

2) Use require_once():
Code:
// must be in the same folder, otherwise change the path!
require_once('Animal.php');
Class Cat extends Animal
{
(...)
}



load a Class that extends a Class ? - El Forum - 03-03-2009

[eluser]junkwax[/eluser]
Thank you


load a Class that extends a Class ? - El Forum - 03-03-2009

[eluser]TheFuzzy0ne[/eluser]
I would suggest the second method, as it will only instantiate the class you're loading via the loader, and not the one it's extending. Unless of course you do want to instantiate both classes.