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