Have question about static modifier in OOP Code. Please see enclosed example code.

I cannot for the life of me understand why the following values are submit to 3.  I thought that the whole point of an extended class was to retain individual values independent of the parent class.  Furthermore, why aren't they all 1 instead of all being three 3?  Is this part of the quirkiness that leads developers to say PHP is too weird and idiosyncratic for its own good (and eventually leads to abandoning ship for other languages like Python)?

  class One {
    static $foo;
  class Two extends One { }
  class Three extends One { }
  One::$foo = 1;
  Two::$foo = 2;
  Three::$foo = 3;
 echo One::$foo;   // 3
  echo Two::$foo;   // 3
  echo Three::$foo; // 3

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Class Two and Three assume the static property of Class One.  They do not redefine or override it in any way.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LB1234Author Commented:
Hi Ray, ok so this behavior only applies to static modifiers, not regular attributes?  And so since 3 was set last, all child classes get that value?
Ray PaseurCommented:
Yes.  Here is the reference on "Static."

The terminology may be a little off-putting in PHP object-oriented notation.  The variables inside an object are called properties.  The functions defined in the class are called methods.

Class One has one property and no methods.  Class Two and Class Three have no properties of their own and no methods.
LB1234Author Commented:
Thanks again, Ray!
Ray PaseurCommented:
Glad to help.  One day soon, I promise, you'll come to love the PHP OOP model.  It's not perfect, but it's so much better than procedural code, that you'll never go back!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.