Req #65750 [Opn->Wfx]: Enabling factory constructors
Edit report at https://bugs.php.net/bug.php?id=65750&edit=1
ID: 65750
Updated by: nikic@php.net
Reported by: thomas dot sahlin at invented dot se
Summary: Enabling factory constructors
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: *General Issues
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Going to explicitly decline this, we're not going to muddy constructor semantics in this
fashion. Please do use a static factory method.
Previous Comments:
------------------------------------------------------------------------
[2013-09-24 08:40:06] thomas dot sahlin at invented dot se
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 this bug report at https://bugs.php.net/bug.php?id=65750&edit=1
Thread (2 messages)