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

Posted on 2014-08-07
Last Modified: 2014-08-07
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

Question by:LB1234
    LVL 107

    Accepted Solution

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

    Author Comment

    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?
    LVL 107

    Expert Comment

    by:Ray Paseur
    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.
    LVL 1

    Author Closing Comment

    Thanks again, Ray!
    LVL 107

    Expert Comment

    by:Ray Paseur
    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now