From: thomas dot sahlin at invented dot se Operating system: PHP version: Irrelevant Package: *General Issues Bug Type: Feature/Change Request Bug description:Enabling factory constructors
Description:
------------
It would be nice to be able to create factory constructors (like Dart
offers).
This could be achieved by allowing __construct to return an arbitrary
object of
the same class or a descendant thereof.
No return statement would then mean "return $this;" as it is today.
Sure, one can use a static factory method, but it's not as elegant and
cannot
simply be added to existing code without modifying all places where objects
are
instantiated.
Test script:
---------------
class MyClass
{
public function __construct($id)
{
if (isset(self::$cache[$id]))
return self::$cache[$id];
// Do expensive setup
self::$cache[$id] = $this;
}
private static $cache = array();
}
--
Edit bug report at https://bugs.php.net/bug.php?id=65750&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=65750&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=65750&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=65750&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=65750&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=65750&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=65750&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=65750&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=65750&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=65750&r=support
Expected behavior: https://bugs.php.net/fix.php?id=65750&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=65750&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=65750&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=65750&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65750&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=65750&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=65750&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=65750&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65750&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=65750&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=65750&r=mysqlcfg
