Search code examples
phpinheritancestaticsubclassextend

PHP: Sub class static inheritance - children share static variables?


As you can see below I have a super class (Article) and two sub classes. I want each of the sub classes to have a static array that shall hold all it's objects.

abstract class Article
{
    public static $articles = array(); // Variable for storing all the objects of each sub-class.

    public function add_Object_To_Array()
    {       
        array_push(self::$articles, $this);
    }
}

class Report extends Article{}
class Interview extends Article{}

-Making two Report objects and adding them to their array:

$tmp = new Report();
$tmp->add_Object_To_Array();

$tmp = new Report();
$tmp->add_Object_To_Array();

-Making two Interview objects and adding them to their array:

$tmp = new Interview();
$tmp->add_Object_To_Array();

$tmp = new Interview();
$tmp->add_Object_To_Array();

print_r(Report::$articles);
print_r(Interview::$articles);

-The above script spits out the two arays:

Array
(
    [0] => Report Object()

    [1] => Report Object()

    [2] => Interview Object()

    [3] => Interview Object()   
)
Array
(
    [0] => Report Object()

    [1] => Report Object()

    [2] => Interview Object()

    [3] => Interview Object()    
)

Which looks pretty similar if you ask me, but the first one should only contain Reports, and the second one only Interviews.

1. It seems that there is only one array, why is it only one array?
2. I have a static container of objects in the same class, is this bad coding? (Any suggestions?)

I'm pretty new to php, but have a background from java.


Solution

  • Everything is going into only one array for two reasons:

    1. The $articles property is only defined in the Article class.

      Static class properties do not get inherited the same way you might expect if you're used to non-static properties. While they are available to the child classes, they're still referencing a single variable on the parent class - resulting in the behavior you're seeing here where both child classes are sharing the same array.

      The only way to prevent this is to define a separate array in each of your child classes, like this:

      class Report extends Article {
          public static $articles = array();
      }
      class Interview extends Article {
          public static $articles = array();
      }
      

      This actually makes sense if you think of the static variable declarations as code that gets run when the class is defined. Creating a static variable and assigning an empty array to it happens when the Article class is defined. It doesn't happen again when the Interview and Report classes are defined. There's only one time that an empty array is getting assigned - there's only one shared variable.

    2. You're using self in your add_Object_To_Array() method instead of static.

      • self:: refers to the class it is defined in, so since your add_Object_To_Array() method is defined in the Article class, it'll refer to the Article::$articles array.

      • static:: Is available starting in PHP 5.3, and refers to the class it is called on. This is known as Late Static Binding, and will result in add_Object_To_Array() referring to either Report::$articles or Interview::$articles depending on the type of the object you're calling it on.

      This code will reference the two arrays that we declared in the first step:

      public function add_Object_To_Array() {
          array_push(static::$articles, $this);
      }