[oclug] PHP instance name -lunch theme
todd.clattenburg at sdc-dsc.gc.ca
todd.clattenburg at sdc-dsc.gc.ca
Thu Jun 17 13:23:50 EDT 2004
I'm new to OOP, I am just starting to learn about it and understand
it. I don't know anyone who knows about OOP, and my learning has
to come from books and websites.
But given your example, I see what you are getting at. What I am
suggesting (remember, I am an amateur at this, and am not trying to
claim otherwise) is that the name of the object be an
automatic/dynamic/magic property (instvar), that is read when you
try and access it. Similar to the way that '$this' always refers to
the current object. In your example:
// at this point inside of the instance
// pepperoni you would have access to
// $this->instance_name which would equal
// "pepperoni".
// -or-
// get_instance_name($this) would return
// "pepperoni".
$meat_topping = $pepperoni;
// at this point inside of the instance
// meat_topping you would have access to
// $this->instance_name which would equal
// "meat_topping".
// -or-
// get_instance_name($this); would return
// "meat_topping".
This behavior would allow something like:
foreach (instance of classname as x)
{
echo x.instance_name;
}
-or-
foreach (instance of classname as x)
{
$myname = get_instance_name(x);
}
I'm just thinking that you are already assigning a name to the instance
by naming it. The redundancy is in having a 'name' property when you
already have a name.
It seems that it would achieve the superclass/subclass arrangement
that you are suggesting without the apparent programming overhead.
I guess I am drawing upon my experience with the automatic variables
in perl.
I can see that you would run into problems if you wanted to have
names for your objects that would not suffice as variable names. In
this case you could have a separate 'name' property.
As I said, I am only trying to learn about OOP, and do not have a
complete understanding of what is available. Or where the lines are
drawn.
Thanks for the explanation.
---------------------------------------------------------------------|
Todd Clattenburg
Programmer / Analyst
Todd.Clattenburg at sdc-dsc.gc.ca
-----Original Message-----
From: oclug-bounces at lists.oclug.on.ca [mailto:oclug-bounces at lists.oclug.on.ca] On Behalf Of Mike Thomas
Sent: Thursday, June 17, 2004 12:47 PM
To: General Membership Discussion List
Subject: Re: [oclug] PHP instance name -lunch theme
todd.clattenburg at sdc-dsc.gc.ca wrote:
>That's a drag, it seems that:
>
>$pepperoni = new topping("pepperoni","crispy and spicy");
>
>is completely redundant.
>
>
The correct OO way to do this would be to have a superclass Topping and
a subclass oPepperoni. You would then do something like new
Pepperoni("cripsy and spicy"). You could have a classVar that had the
topping name.
What language do you use that computes charactersitics of the object
being created basd on the name of the variable you store it in?
If I do something like:
$meat_topping = $pepperoni;
unset($pepperoni);
What should the "name" be then?
Mike.
>Thanks.
>
>---------------------------------------------------------------------|
>Todd Clattenburg
>Programmer / Analyst
>Todd.Clattenburg at sdc-dsc.gc.ca
>
>
>-----Original Message-----
>From: oclug-bounces at lists.oclug.on.ca [mailto:oclug-bounces at lists.oclug.on.ca] On Behalf Of Mike Thomas
>Sent: Thursday, June 17, 2004 12:18 PM
>To: General Membership Discussion List
>Subject: Re: [oclug] PHP instance name -lunch theme
>
>
>todd.clattenburg at sdc-dsc.gc.ca wrote:
>
>
>
>>I am trying to access the instance name of an object in PHP. For
>>example, say I have a class called 'topping'.
>>
>>$pepperoni = new topping("crispy and spicy");
>>$cheese = new topping("ooey gooey");
>>
>>Now I want to be able to loop through all of the toppings, and
>>print out something like:
>>
>> pepperoni - crispy and spicy
>> cheese - ooey gooey
>>
>>My question is, is there a function in php to pull out the
>>instance name of a class, or do I have to have a 'name' property
>>of each class, and 'redundantly' populate it in each instance?
>>
>>
>>
>>
>
> There is really no such thing as an instance name. As you suggest, you
>would need to have a instvar (property) called name to hold onto that
>sort of information. In the contructor for the class you would then set
>the bame of $this->$name to the parameter supplied to the contructor.
>
> Mike.
>
>
--
OCLUG general discussion list
OCLUG at lists.oclug.on.ca
http://www.oclug.on.ca/mailman/listinfo/oclug
More information about the OCLUG
mailing list