first time using multiple instances of an object

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

first time using multiple instances of an object

Post by Luke »

I know this sounds crazy, but I have been really trying to move towards OOP (obviously with my 10 or so OOP threads started lately) and I still have never used instances of an object for anything. So far, I haven't had a use for instances. I think I may have finally ran into an instance where I need to use instances :?

I am building a user administration panel for my file manager. Now, I have a user_admin object which basically runs queries on the user database to show all users, etc. Now, when I work with users, I would like to build another object called users, and each instance of this object will be a different user with methods such as delete(), add(), modify() etc.

My question is pretty basic... is this the proper place to use instances? If so, any advice on just how to go about this? Should I make a property for every privilege the user has ("create directory", "delete directory", "create file" etc.) or should I assign these values to an array? Thanks in advance and once again I am sorry for my crappy explanation of the problem.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

wait, instance?

$thingy = new ThingyClass();

...?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yes
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yes.. well I guess I should say it's my first time using multiple instances

edit... oops I didn't mean to post twice (sorry mods)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

An instance of an object is just the result of instantiation (the NEW keyword).

If you need a "collection" of users then yes, I'd probably contain them in an array. At least then you can do things like:

Code: Select all

foreach ($this->userCollection as $user)
{
    $user->doSomething();
}
To be honest I'm not entirely sure what you're asking though ;)
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Re: first time using multiple instances of an object

Post by TheMoose »

The Ninja Space Goat wrote:My question is pretty basic... is this the proper place to use instances? If so, any advice on just how to go about this? Should I make a property for every privilege the user has ("create directory", "delete directory", "create file" etc.) or should I assign these values to an array? Thanks in advance and once again I am sorry for my crappy explanation of the problem.
1] It depends on what your class looks like. If there is nothing that requires initiazaton or a value that you don't have to pass to the object in some form, then no you don't necessarily need an instance.

IE:
myUser->IsLoggedIn(); versus userClass::IsLoggedIn('username');

The first one requires an instance because it is dependant on the object to have the username, the second does not because the username is passed as a value to the function.

2] Use an array, much easier, and allows for growth, otherwise you would have to modify the code everytime you add a new privilege.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I guess my question is really: When is it appropriate to use multiple instances of a class?
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

http://www.zend.com/zend/tut/class-intro.php
That is, a class function must be referred to as "object->function." This syntax is a means for getting the function you want, since "$object1->function" and "$object2->function" might be two totally different functions.

Alternatively, you can call a function (a method) using the syntax Class::function($vars), thereby leaving the "$this" variable unset. Here, "$vars" are the function's parameters.
so you use the SRO ( :: )[forgot about the text parser. it should be this] if you don't want to use the '$this' pointer. i don't know what it's considered in PHP. it's not a pointer but a....? meh. doesn't really matter much
Last edited by MrPotatoes on Wed Jun 07, 2006 1:57 pm, edited 1 time in total.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

MrPotatoes wrote:http://www.zend.com/zend/tut/class-intro.php

so you use the SRO (::) if you don't want to use the '$this' pointer. i don't know what it's considered in PHP. it's not a pointer but a....? meh. doesn't really matter much
I believe it's just a reference :)
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

MrPotatoes wrote:http://www.zend.com/zend/tut/class-intro.php
That is, a class function must be referred to as "object->function." This syntax is a means for getting the function you want, since "$object1->function" and "$object2->function" might be two totally different functions.

Alternatively, you can call a function (a method) using the syntax Class::function($vars), thereby leaving the "$this" variable unset. Here, "$vars" are the function's parameters.
so you use the SRO ( :: )[forgot about the text parser. it should be this] if you don't want to use the '$this' pointer. i don't know what it's considered in PHP. it's not a pointer but a....? meh. doesn't really matter much
Well, what they're saying is, you can't use the $this keyword (which is the reference to the currently instantiated object), because there is no instantiation. There literally is no $this reference. You'll note that article was written well before Zend engine 2 (essentially, PHP5) and so more appropriate OOP capabilities, i.e. static had not been available. In a nutshell, when calling Object::method(), method() should be declared static (class method vs object method).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The Ninja Space Goat wrote:I guess my question is really: When is it appropriate to use multiple instances of a class?
The easy answer is when you have many of the same thing. Let me give you a simple example of a place I use multiple instances of a class. I have a form controller that manages HTML/HTTP forms. The forms obviously have multiple parameters (aka fields). I create an instance of a Parmeter class for each parameter/field in the form. I do this to encapsulate the data and behaviors of a form parameter/field. For example they all have a value and a type. They also have filtering and validation unique to each. If there is an associated database then their is the name if the field in the table, plus any code that gets run after load or before save.

A database connnection that returns record sets is another example. There are many.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

For multiple objects (an instantiated class = an object) and not multiple references...

An object can be many things, a unit of functionality and such. If you consider something basic, say a user, as an object then you will need multiple User objects. Take an application which is OOP based and uses some form of ORM. I could have:

Code: Select all

// User/Message are "data objects" representing database rows
// Send a message from User 1 to User 2

$user1 = new User();
$user1->getByPk(1);

$user2 = new User();
$user2->getByPk(2);

$message = new Message();
$message->setSenderId( $user1->getId() );
$message->setRecipientId( $user2->getId() );
$message->setSubject('Greetings...');
$message->setText('Greetings to User 2 from User 1!');
$message->save();
Fairly simple usage... Could have dozens of User objects at once, or dozens of Message objects when creating an Inbox view...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

is it possible to do something like this?

Code: Select all

foreach($user_ids as $id){
    $user[$id] = new user($id);
}

$user[23]->some_method();
Because I would like to have a class called user_admin that perfoms all sorts of global operations on the entire users database. Then I would like to call every user from the database, and create an instance of class "user" for each one (the user class would do operations on individual users)
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Yes, you can. Just make sure that if the code piece you posted is inside your user_admin class, to not forget the $this identifier on any of those variables that belong to the class. :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

is there any reason why I should or shouldn't do that?
Post Reply