Hi Marco,

the only reason for prohibiting explicit __construct calls is that it makes PHP 
objects mutable and it's state unpredictable. Still would like to give this RFC 
a try.

And what about polymorphic dispatch?

Regards,
Tim

> On 12 Jan 2017, at 21:35, Marco Pivetta <ocram...@gmail.com> wrote:
> 
> Heya,
> 
> While I agree that it is weird to be able to call constructors more than 
> once, this is generally used for:
> 
>  * lazy loading
>  * resource reset
> 
> Specifically, what is going on is something like following:<?php
> 
> final class DbConnection
> {
>     private $dsn;
>     private $initializer;
>     public function __construct(string $dsn)
>     {
>         $this->dsn = $dsn;
>         // socket stuff happens here, much like with PDO
>     }
> 
>     public function query(string $queryString) : array
>     {
>         ($this->initializer)();
>         // irrelevant from here on
>         return ['query' => $queryString, 'dsn' => $this->dsn];
>     }
> 
>     public static function lazyInstance(string $dsn) : self
>     {
>         $instance = (new 
> ReflectionClass(self::class))->newInstanceWithoutConstructor();
>         $instance->initializer = function () use ($dsn, $instance) {
>             $instance->__construct($dsn);
>             $instance->initializer = function () {
>             };
>         };
>         return $instance;
>     }
> }
> 
> $instance = DbConnection::lazyInstance('mysql://something');
> 
> var_dump($instance);
> 
> var_dump($instance->query('SELECT * FROM foo'));
> var_dump($instance->query('SELECT * FROM bar'));
> 
> Here's an example of it at work: https://3v4l.org/Y0eoL 
> <https://3v4l.org/Y0eoL>
> 
> The pattern is simple:
> 
>  * intercept constructor call
>  * capture constructor parameters
>  * instantiate without constructor
>  * defer constructor call for later
> 
> The same can be used in a myriad of different ways, but this is a legit 
> use-cases that generally don't involve coding everything into the same class 
> (and I generally advise against doing that anyway).
> 
> Therefore I don't see a reason to drop manual constructor calls, unless there 
> is a strong necessity to get rid of 'em.
> 
> 
> 
> Marco Pivetta 
> 
> http://twitter.com/Ocramius <http://twitter.com/Ocramius>      
> 
> http://ocramius.github.com/ <http://ocramius.github.com/>
> 
> On Thu, Jan 12, 2017 at 8:11 AM, Tim Bezhashvyly <tim.bezhashv...@gmail.com 
> <mailto:tim.bezhashv...@gmail.com>> wrote:
> Dear internals,
> 
> I would like to propose 2 RFCs:
> 
> - Disallow explicit call of __construct method
> - Polymorphic dispatch
> 
> I'm sure I'm not the first who came with those 2 ideas so in case those were 
> already proposed and rejected just let me know.
> 
> Otherwise please bless me with mana which will allow me to submit them.
> 
> Regards,
> Tim
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php 
> <http://www.php.net/unsub.php>
> 
> 

Reply via email to