CodeIgniter Forums
Using normal PHP classes - 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: Using normal PHP classes (/showthread.php?tid=2302)



Using normal PHP classes - El Forum - 07-28-2007

[eluser]robert.isenberg[/eluser]
Hi,

I've got a quite a lot of experience using codeigniter to develop web sites, but I'm taking over work on a web application that has some pre-existing PHP classes. I want to re-use these classes and just plug them into Codeigniter. Is this possible?

Is it just a matter of doing:

include_once("class_name")

wherever I need it, whether that be in a Controller class or Model class etc?

Any thoughts would be greatly appreciated.


Using normal PHP classes - El Forum - 07-29-2007

[eluser]Phil Sturgeon[/eluser]
Where you put it depends on what the class is, most are likly to be a library though. You could do it vanilla PHP style but theres not much point as we have this lovely framework already provided for doing such things.

Just chuck it in the library folder and call it like any other lib, you shouldnt get too much trouble but post it back here if you do.


Using normal PHP classes - El Forum - 07-29-2007

[eluser]robert.isenberg[/eluser]
Thanks for that.

How would I instantiate an instance of a class then with code igniter? Let's say I have some existing code like this:

$object_array = array();

while (some condition) {
$object_array[] = new Widget();
}

What's the equivalent if the Widget class became a code igniter library (or model). Thanks!


Using normal PHP classes - El Forum - 07-29-2007

[eluser]Phil Sturgeon[/eluser]
Creating Your Own Libraries :p


Using normal PHP classes - El Forum - 07-29-2007

[eluser]robert.isenberg[/eluser]
Hi, I have read that before, and maybe I'm being dumb, but it's still not entirely clear to me how you instantiate multiple objects from a library.

As far as I can tell you use $this->load->library() to get one instance of a library, but doesn't this get a single instance of that library? Do subsequent calls get a fresh instance? I thought they were just passing back the *same* instance as on the first call to this.

Perhaps you can point me to the exact part on that help page that tells me what I need?

Thanks!


Using normal PHP classes - El Forum - 07-29-2007

[eluser]Crafter[/eluser]
You could use plugins, which will have the effect of includ()ing your class defintion. You could then use multiple instances of the classes.
You would have to rename your class file name, though (my_class_pi.php).


Using normal PHP classes - El Forum - 07-29-2007

[eluser]robert.isenberg[/eluser]
Thanks for that... but I've just looked at the Plugin section of the userguide at http://www.ellislab.com/codeigniter/user-guide/general/plugins.html and again, it's still not obvious to me how you actually create multiple instances.

Are you saying that I load he plugin:

$this->load->plugin('my_class_pi.php');

and that then I can use:

$an_object = new MyClassPi();

where my_class_pi.php is:

class MyClassPi(){

... etc ..

}

Is that what you mean? Sorry if I'm being obtruse!


Using normal PHP classes - El Forum - 07-29-2007

[eluser]Crafter[/eluser]
Yes. The last section on the manual page says:
"Once you've loaded the Plugin, you'll call it the way you would a standard PHP function."

EDIT:
The page also says on the first line : "...main difference is that a plugin usually provides a single function...". but does it really matter? Not to me!