#46286 [Opn->Bgs]: Extending class with static getInstance()-type method
| From: | colder@php.net | Date: | Mon, 13 Oct 2008 18:38:41 +0000 |
| Subject: | #46286 [Opn->Bgs]: Extending class with static getInstance()-type method | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-129671@lists.php.net to get a copy of this message | ||
ID: 46286
Updated by: colder@php.net
Reported By: bob at db dot org
-Status: Open
+Status: Bogus
Bug Type: Feature/Change Request
Operating System: Any
PHP Version: 5.3.0alpha2
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
retrurn new static();
Previous Comments:
------------------------------------------------------------------------
[2008-10-13 18:14:10] bob at db dot org
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 this bug report at http://bugs.php.net/?id=46286&edit=1