Req #65750 [Opn->Wfx]: Enabling factory constructors

From: Date: Fri, 23 Jul 2021 09:31:11 +0000
Subject: Req #65750 [Opn->Wfx]: Enabling factory constructors
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235293@lists.php.net to get a copy of this message
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)

« previous php.bugs (#235293) next »