CodeIgniter Forums
OOP? - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived General Discussion (
+--- Thread: OOP? (/thread-49033.html)

OOP? - El Forum - 02-06-2012

Hey there!

I've been using CodeIgniter for a little bit now, but I'm still learning quite a bit. However, I've recently run into something of a hiccup... OOP. How would one go about doing object-oriented programming with CodeIgniter?

I understand that CodeIgniter is OOP in and of itself. However, what if I want to make, say, a member class. Where would I put it? How would I load new members? With libraries, for example, I know that I can load a library, but how would I write something along the lines of "$person = new Member($member_id);" ?


OOP? - El Forum - 02-06-2012

Extending 'CI_Model' these represent objects that act a a 'middleman' for interacting with the db. Take a quick look at MVC design patterns to get an overview of it.

OOP? - El Forum - 02-06-2012

You would create a "members" class and put it in the /application/libraries directory.

Instead of: $person = new Member($member_id);”
You would:
$this->load->library('members', $member_id);  //$member_id gets passed to the __construct method of the members class.

$person = $this->load->library('members', $member_id, 'person');
Please see the User Guide for Creating Libraries for more info. You should also read up on the loader class, if you want your new object returned as a variable instead of extending the CI superobject. The rest of the user guide has some interesting things to read as well. Strange, I know.

OOP? - El Forum - 02-06-2012

That second example looks like it's what I want. I was unaware you could load something directly into a variable that way. Thanks!