PHP 8.5.0 Alpha 4 available for testing

Voting

: min(nine, six)?
(Example: nine)

The Note You're Voting On

webmaster at removethis dot weird-webdesign dot de
15 years ago
On PHP 5.2.x or previous you might run into problems initializing static variables in subclasses due to the lack of late static binding:

<?php
class A {
protected static
$a;

public static function
init($value) { self::$a = $value; }
public static function
getA() { return self::$a; }
}

class
B extends A {
protected static
$a; // redefine $a for own use

// inherit the init() method
public static function getA() { return self::$a; }
}

B::init('lala');
echo
'A::$a = '.A::getA().'; B::$a = '.B::getA();
?>

This will output:
A::$a = lala; B::$a =

If the init() method looks the same for (almost) all subclasses there should be no need to implement init() in every subclass and by that producing redundant code.

Solution 1:
Turn everything into non-static. BUT: This would produce redundant data on every object of the class.

Solution 2:
Turn static $a on class A into an array, use classnames of subclasses as indeces. By doing so you also don't have to redefine $a for the subclasses and the superclass' $a can be private.

Short example on a DataRecord class without error checking:

<?php
abstract class DataRecord {
private static
$db; // MySQLi-Connection, same for all subclasses
private static $table = array(); // Array of tables for subclasses

public static function init($classname, $table, $db = false) {
if (!(
$db === false)) self::$db = $db;
self::$table[$classname] = $table;
}

public static function
getDB() { return self::$db; }
public static function
getTable($classname) { return self::$table[$classname]; }
}

class
UserDataRecord extends DataRecord {
public static function
fetchFromDB() {
$result = parent::getDB()->query('select * from '.parent::getTable('UserDataRecord').';');

// and so on ...
return $result; // An array of UserDataRecord objects
}
}

$db = new MySQLi(...);
UserDataRecord::init('UserDataRecord', 'users', $db);
$users = UserDataRecord::fetchFromDB();
?>

I hope this helps some people who need to operate on PHP 5.2.x servers for some reason. Late static binding, of course, makes this workaround obsolete.

<< Back to user notes page

To Top