Hi,
Stanislav Malyshev wrote:
Could you describe a couple of use cases where you would use such
things (i.e. in real world scripts/libraries)?
Sure, let's take an example:
You have a library that uses a factory pattern, but you'd like to cache
those already generated objects in a static variable in the class. Now,
this feature is required in many classes and you'd not like to duplicate
too much code, so a solution would be:
class MySuperClass {
private static $_objectsLoaded = array();
public static function factory($pkId, $classname = __CLASS__) {
if(isset($_objectsLoaded[$classname][$pkId])) {
return $_objectsLoaded[$classname][$pkId];
}
$o = new $classname();
//load stuff from the database
$table = $classname::$_table;
$pkField = $classname::$_pkField;
// build query using $table, and $pkField, load stuff into $o, etc..
if(!is_array(self::$_objectsLoaded[$classname])) {
self::$_objectsLoaded[$classname] = array();
}
self::$_objectsLoaded[$classname][$pkId] = $o;
return $o;
}
public static function create($classname = __CLASS__) {
$o = new $classname();
// ...
return $o;
}
}
class MyClass extends MySuperClass {
protected static $_pkField = 'id';
protected static $_table = 'theTable';
public static factory($pkId, $classname = __CLASS__) {
return parent::factory($pkId,$classname);
}
public static create($classname = __CLASS__) {
return parent::create($classname);
}
// ...
}
There may be some typos in the code above that makes its syntax
incorrect, but the idea is here.
Now, as you can see, because late static binding is not yet implemented
and probably never will be because of the overhead it might introduce,
you've to pass the classname as a parameter and use it to request things
from the child's class, at this point, being able to do that using that
$classname:: shortcut is quite elegant, instead of having to use
overkill call_user_func calls as all those _get*() methods would have to
be redefined in every the child classes.
I wouldn't mind having to use multiple call_user_func calls for that
purpose if I knew it was too hard to implement that $classname:: thing,
but it's so easy that I really believe it's a feature worth having. And
PHP is also about multiple ways of doing something, right ? :)
--
Etienne Kneuss
http://www.colder.ch
[EMAIL PROTECTED]
Men never do evil so completely and cheerfully as
when they do it from a religious conviction.
-- Pascal
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php