From: bob at db dot org
Operating system: Any
PHP version: 5.3.0alpha2
PHP Bug Type: Feature/Change Request
Bug description: Extending class with static getInstance()-type method
Description:
------------
A getInstance() method is a fairly common design-pattern, yet it's
impossible to create such a function in a base-class and have it function
as expected when access via the inheriting class.
I understand that this is documented behavior, and that it's probably
impossible to achieve this using existing syntax/functions. What I'm hoping
for is a new function; something like get_statis_class(void) that will
return the name of the class used to *access* the static method, not the
name of the class in which it was defined.
I can provide more elaboration on why this is a nice feature if needed.
The attached example is kept short to illustrate the problem as clearly as
possible.
Reproduce code:
---------------
<?php
class base {
public static function getInstance() {
$class = __CLASS__;
return new $class();
}
}
class heir extends base {
}
$instance = heir::getInstance();
die(get_class($instance));
?>
Expected result:
----------------
heir
Actual result:
--------------
base
--
Edit bug report at http://bugs.php.net/?id=46286&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=46286&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=46286&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=46286&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=46286&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=46286&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=46286&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=46286&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=46286&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=46286&r=support
Expected behavior: http://bugs.php.net/fix.php?id=46286&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=46286&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=46286&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=46286&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=46286&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=46286&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=46286&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=46286&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=46286&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=46286&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=46286&r=mysqlcfg