Page 1 of 1

static inside a class method

Posted: Tue Feb 06, 2007 11:25 pm
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 :)

Posted: Tue Feb 06, 2007 11:26 pm
by RobertGonzalez
First rule of professional developers... try it!

But to answer your question, yes, static works inside of class methods.

Posted: Tue Feb 06, 2007 11:37 pm
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?

Posted: Wed Feb 07, 2007 12:39 am
by feyd
Just try it.

Or search.. this concept has been talked about recently.

Posted: Wed Feb 07, 2007 11:06 am
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.