Quantcast
Viewing all articles
Browse latest Browse all 11

Answer by Emanuel Landeholm for How to initialize static variables

If you have control over class loading, you can do static initializing from there.

Example:

class MyClass { public static function static_init() { } }

in your class loader, do the following:

include($path . $klass . PHP_EXT);if(method_exists($klass, 'static_init')) { $klass::staticInit() }

A more heavy weight solution would be to use an interface with ReflectionClass:

interface StaticInit { public static function staticInit() { } }class MyClass implements StaticInit { public static function staticInit() { } }

in your class loader, do the following:

$rc = new ReflectionClass($klass);if(in_array('StaticInit', $rc->getInterfaceNames())) { $klass::staticInit() }

Viewing all articles
Browse latest Browse all 11

Trending Articles