Page 1 of 3
first time using multiple instances of an object
Posted: Wed Jun 07, 2006 1:05 pm
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.
Posted: Wed Jun 07, 2006 1:11 pm
by MrPotatoes
wait, instance?
$thingy = new ThingyClass();
...?
Posted: Wed Jun 07, 2006 1:15 pm
by Luke
yes
Posted: Wed Jun 07, 2006 1:16 pm
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)
Posted: Wed Jun 07, 2006 1:17 pm
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

Re: first time using multiple instances of an object
Posted: Wed Jun 07, 2006 1:24 pm
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.
Posted: Wed Jun 07, 2006 1:41 pm
by Luke
I guess my question is really: When is it appropriate to use multiple instances of a class?
Posted: Wed Jun 07, 2006 1:48 pm
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
Posted: Wed Jun 07, 2006 1:53 pm
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

Posted: Wed Jun 07, 2006 2:12 pm
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).
Posted: Wed Jun 07, 2006 2:34 pm
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.
Posted: Wed Jun 07, 2006 3:02 pm
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...
Posted: Wed Jun 07, 2006 3:34 pm
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)
Posted: Wed Jun 07, 2006 3:44 pm
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.

Posted: Wed Jun 07, 2006 3:45 pm
by Luke
is there any reason why I should or shouldn't do that?