Welcome Guest, Not a member yet? Register   Sign In
Best practices using multiple object instances?
#1

[eluser]tarsusc81[/eluser]
Something I've observed since starting with CodeIgniter is that while it is perfectly true that the framework is "object-oriented," I might be apt to call it "class-oriented." What I mean is that the architecture centers around encapsulating functionality in a class, and then loading a single instance of that class to use its functionality.

That's as it should be. But things get a little murky for me when trying to deal with classes intended for actually creating and using objects. (Meaning multiple instances.)

For example (forgive me if my syntax has errors; I'm a little rusty):
Code:
class User extends Model
{
     function save($userID, $data)
     {
          //SQL query
     }
}

This is designed to be stored using the convention User.php and to be loaded and used as a single instance:

Code:
$this->load->model('User');
$this->User->save(324, $data['324']);
$this->User->save(450, $data['450']);

But what if I actually want a User class that corresponds to a single user, with multiple instances holding data for each?

Code:
class User
{
     protected var $id;

     function save($data)
     {
          //SQL query
     }
}
...
$user1 = new User(324);
$user1.save($data['324']);
$user2 = new User(450);
$user2.save($data['450']);

This may not be the best example, but hopefully it gets the idea across.

I have trouble conceptualizing how to use multiple-instance objects leveraging the functionality of CodeIgniter, rather than breaking out of it. (I.e., having to use a normal "require()" instead of "$this->load")

Any advice from those who've created object-heavy applications? What's the best way to get the most out of CodeIgniter when doing so?

It's occurred to me to do something like actually declaring two classes in one model or library file - one as the "master" class to be used with "$this->load" and possibly have methods to interact with the second class, which is designed to have multiple instances. But I don't know if this is a good idea, or if there's a better one.


Messages In This Thread
Best practices using multiple object instances? - by El Forum - 07-28-2010, 08:49 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 09:07 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 09:26 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 09:35 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 12:04 PM
Best practices using multiple object instances? - by El Forum - 07-31-2011, 09:20 PM
Best practices using multiple object instances? - by El Forum - 08-01-2011, 01:34 AM
Best practices using multiple object instances? - by El Forum - 08-11-2011, 06:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB