LonghornPHP 2026

Voting

: min(eight, five)?
(Example: nine)

The Note You're Voting On

michalf at ncac dot torun dot pl
21 years ago
Inheritance with the static elements is a nightmare in php. Consider the following code:

<?php
class BaseClass{
    public static $property;
}

class DerivedClassOne extends BaseClass{
}

class DerivedClassTwo extends BaseClass{
}

DerivedClassOne::$property = "foo";
DerivedClassTwo::$property = "bar";

echo DerivedClassOne::$property; //one would naively expect "foo"...
?>

What would you expect as an output? "foo"? wrong. It is "bar"!!! Static variables are not inherited, they point to the BaseClass::$property.

At this point I think it is a big pity inheritance does not work in case of static variables/methods. Keep this in mind and save your time when debugging.

best regards - michal

<< Back to user notes page

To Top