Welcome Guest, Not a member yet? Register   Sign In
Codeigniter and model instantiation
#1

[eluser]The Mask[/eluser]
Hi,
I am a little confused about how Codeigniter instantiates models. Is there some good documentation about this anywhere?

E.g. If I have a class called ‘dog’, I would use Codeigniter like…
$this->load->model( ‘Dog’, ‘’, TRUE );
$this->Dog->method() or $this->Dog->property

But I would have expected:
$myDog = new Dog();
$myDog->method();

$anotherDog = new Dog();
$anotherDog->property

So when does the instantiation work using Codeigniter and what would I do if I wanted a number of Dog() objects?

Thanks
#2

[eluser]TheFuzzy0ne[/eluser]
The loader should only be used for when you want just one instance of a library/model. If you want more, then you can use the new keyword, and not bother loading it with the CodeIgniter loader, or you can have your model return objects instead if you wanted to.
#3

[eluser]Unknown[/eluser]
Quote:If you want more, then you can use the new keyword, and not bother loading it with the CodeIgniter loader

If I use the new keyword and don't use the CI loader, then where is the best place to include the file that has my model class since CI won't know anything about it? In the controller file?

Quote:or you can have your model return objects instead if you wanted to.

Can you provide a quick example of what this looks like?
#4

[eluser]TheFuzzy0ne[/eluser]
You can simply include the file before instantiating it, just as the loader would:
Code:
inlude APPPATH.'libraries/some_library'.EXT;

I could provide an example, but one already exists. The database/Active Record class returns result objects.




Theme © iAndrew 2016 - Forum software by © MyBB