CodeIgniter Forums

Full Version: OOP?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
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);" ?

Thanks!

El Forum

[eluser]aleul[/eluser]
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.

El Forum

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

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

//or
$person = $this->load->library('members', $member_id, 'person');
$person->do_something();
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.

El Forum

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