0

Trying to set an array and a count, which are both static members, by referencing them after getting the type of the current class. The intention is to have a global list of all items of a particular type that I have loaded, but I would like them to be split up into the static properties for each particular class. The issue is that I will be inheriting a few classes from a base class, but I want the static property in the 'child' class to get appended to.

Here's what I'm shooting for, give or take:

$Class = get_class($this);
($Class)::$List[($Class)::$Count++] = $this;
outis
  • 75,655
  • 22
  • 151
  • 221
DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
  • Note that SO is a Q&A site, not a forum. Answers should be posted separately from questions. Also, your code doesn't add anything to what VolkerK's does. You don't need a static `$Count` variable, as assigning to [empty brackets](http://php.net/array) (`$List[] = $this`) appends to an array. Also, you don't need to override constructors just to call the parent constructor. – outis Jul 11 '12 at 22:22

1 Answers1

1

In PHP 5.3 and later, you can use late static binding to access overridden static variables. The short version is that you use static rather than self or the class name when accessing static variables. For example:

<?php
abstract class A {
    public function __construct() {
        static::$List[] = $this;
    }
}

class B extends A { 
    public static $List = array();
}

class C extends A {
    public static $List = array();  
}
$b = new B();
$c1 = new C();
$c2 = new C();
$c3 = new C();

echo '#objects of class C: ', count(C::$List), "\n";
var_dump(C::$List);

Keep in mind that if a child class defines a constructor, it must explicitly call the parent's constructor.

outis
  • 75,655
  • 22
  • 151
  • 221
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Already considered and accounted for; if this works, much thanks. – DigitalJedi805 May 08 '12 at 07:10
  • Beautiful. Much appreciated Sir. – DigitalJedi805 May 08 '12 at 07:12
  • I'd keep the $Count variable out and use $List[] and count($List), but that's probably a matter of choice... – VolkerK May 08 '12 at 07:45
  • Otherwise seems like class B will get an independent list from class... C, assuming C is identical to B? – DigitalJedi805 May 08 '12 at 07:52
  • Is it true that 'using my edited code, if I create a class C that is identical to class B, they will have independent lists'? – DigitalJedi805 May 08 '12 at 20:42
  • sorry, I still don't understand "a class C that is identical to class B". Do you mean `class C extends B { /* nothing in here */ }` ? But yes, two distinct classes will have/use two distinct lists. – VolkerK May 09 '12 at 06:39
  • I mean 'class C extends A{ }'; but I answered my own question; if class A has a List, and classes B and C both use parent::__construct to add to static::$List, all of the elements will be placed in the 'classAstatic::$List'. If however, I add a $List property to the subclasses, I.E. 'class D extends A{ public $List = null; }'; then elements of type D will be separated from elements of type A B and C. – DigitalJedi805 May 09 '12 at 16:50
  • @DigitalJedi805 you may find http://stackoverflow.com/questions/3187124/peculiar-behaviour-with-php-5-3-static-inheritance-and-references useful for explaining how static class members work with regards to inheritance. – connec May 09 '12 at 17:11