[oclug] PHP instance name -lunch theme
Mike Thomas
mike at bigrideau.com
Thu Jun 17 12:47:14 EDT 2004
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.
>
>
More information about the OCLUG
mailing list