|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-07-23 09:31 UTC] nikic@php.net
-Status: Open
+Status: Wont fix
[2021-07-23 09:31 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 02:00:01 2025 UTC |
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(); }