Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>Demo::$name</code> and <code>self::$name</code> from within the <code>Demo</code> class is, in effect, the same. You can access it by name due to the fact that the the variable is static and accessible, just like you'd be able to access a <em>public</em> static variable from a different class using the classes' name, for example.</p> <p>So in that case, accessing by name is mostly just a by-product of being able to access <em>any</em> public static variable from an accessible class.</p> <p>The second output produces the same as the first because you're apparently creating a new <code>Person1</code> object, not a <code>Person2</code> object (see: <code>$p2=new Person1("xxx", "yyy");</code>)</p> <p>If you were to create a <code>Person2</code> object, then you'd get an error, because you can't assign values to a static variable that hasn't been declared (you've got object-level variables in <code>name</code> and <code>address</code>, but not static). If you declared them as <code>static</code>, then you'd get the same output from the modified <code>show</code> method due to the fact that it's using a static call.</p> <p>If you're not sure what the difference between object (read: instance) variables and static variables are, then i encourage you to partake in some Googling, but basically static variables exist at the class level, not the object level, and are accessible from any class method or object of the specified type (so if you had 100 instances of <code>Person2</code> they'd all have access to the <em>same</em> <code>name</code> variable, for example), whereas instance variables are unique to the individual object itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload