static inside a class method

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

static inside a class method

Post by alex.barylski »

Do they retain their value across object instances as well?

Basically...if I have two objects derived form one parent class which has a function which uses a static to keep count...

Do the two child objects share the same static or does a new static get created for each object?

I've read the PHP docs but it makes no mention of this...one way to find out would be to test it...but incase someone already knows or cares to test for me...I would appreciate it :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

First rule of professional developers... try it!

But to answer your question, yes, static works inside of class methods.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Everah wrote:First rule of professional developers... try it!

But to answer your question, yes, static works inside of class methods.
Second rule of professional developers - learn from others mistakes :P

I could try it...or ask someone else if they have... :)

I'm not sure you understood me totally Everah...I know statics work inside methods...but if two object derive from a single parent class (nothing unusual) and that parent class defines a function/method which has uses a local static (not static member) does that static keeps it's value unique to the object it belongs or does the the static use the same memory space across the board (both objects, although different instances (in PHP5 anyways) use the same memory for that base method static???

See what I'm asking now?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just try it.

Or search.. this concept has been talked about recently.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Honestly, my thought is to try it. Write a small class with a single method, instantiate it two times over and see what happens.
Post Reply