Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97737 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38295 invoked from network); 12 Jan 2017 21:52:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2017 21:52:50 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.51 mail-wm0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:38365] helo=mail-wm0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 38/EB-50165-0BAF7785 for ; Thu, 12 Jan 2017 16:52:49 -0500 Received: by mail-wm0-f51.google.com with SMTP id r144so41928774wme.1 for ; Thu, 12 Jan 2017 13:52:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=OWGbdHDI749TyAXxpd9ppa3g/6s2YvRHBE7zL6OwzZ4=; b=TYsnaztk6/Q3Z7LTL3FRX3PhphvbVl2rfy9dmBKVudyT9xqTUSHHirexeJWy4yiNNT Bv1Nzp8gdXwprmwkVwreAhw+xFcV7rkjdKA521S+0wCmIUWACe+Mk2y4zAq3k7CBOxrE Bi/mSqW9qaErzvaM9QhUwPVXJmpNvBoY355vJ9sygn/Nb98auDJTTscIRTuQONfiIVFv DYKZmcvXhutRcwfVjLP4NgPjCdF8PNadkF1/Phz7HSvJdf/xFquICiVO99jLgI1WfFrT c8/8+r+LpIeRBfmwBaCRfuO5/3lg5KNfu+ZJijzAGzP1V0FYbf7Dus+XLv3sHVSEWU31 CkbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=OWGbdHDI749TyAXxpd9ppa3g/6s2YvRHBE7zL6OwzZ4=; b=QOJRZX8qm2BTP1wU8dj4eauNN5xhduID9Poh0DaqTO5SLq4L4lr4gbDvh5ZL5ZvPWI MK1uw7rSlAtp3kZjRzCVNbAdQPP6BiEBLdzD5iNGvI/tBxPXoCRuB4J/ZS0Gnqao50JG n9GkUTtRat51hXewhTs8HAqh26dj/TG/cTXnPrAzhTbJxbxjpjFgAVQHaqmstn9x/xzg qc4CcaLJ4Pm+R5qhWGOlV8pvESr94zYpLZHrVgHBdF3lAEaYTewYKUXcMtaA73sSHz5h FnbvtIes5TBPoVj3Z+9O1g8QdDUjM/jBDcIqpSOVKGLcPeW3HmH5Wvywmrqu3NfHioRq gjwA== X-Gm-Message-State: AIkVDXKw0gLKOssndrBsY6fBoNZkfNGSpe9c4uDbGQREby31c9HlcYbg8L//B1XDYAKPbHs4SN/IH9QwmCBGgw== X-Received: by 10.223.154.132 with SMTP id a4mr10041548wrc.188.1484257965924; Thu, 12 Jan 2017 13:52:45 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.34.197 with HTTP; Thu, 12 Jan 2017 13:52:25 -0800 (PST) In-Reply-To: <910A374A-8C98-4F60-918D-CF0012FB7518@gmail.com> References: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> <910A374A-8C98-4F60-918D-CF0012FB7518@gmail.com> Date: Thu, 12 Jan 2017 22:52:25 +0100 Message-ID: To: Tim Bezhashvyly Cc: PHP Internals List Content-Type: multipart/alternative; boundary=f403045f5580d067440545ecbca5 Subject: Re: [PHP-DEV] Explicit constructor call and polymorphic dispatch From: ocramius@gmail.com (Marco Pivetta) --f403045f5580d067440545ecbca5 Content-Type: text/plain; charset=UTF-8 Hey Tim, On Thu, Jan 12, 2017 at 10:51 PM, Tim Bezhashvyly wrote: > Hi Richard, > > thank you for support with __construct. I absolutely agree that it breaks > BC and fine to propose it just for PHP 8. > > By polymorphic dispatch I mean a possibility to have the same method > (including constructor) in multiple variations. E.g.: > > class Price > { > public function __construct(int $price) > { > ... > } > > public function __construct(string $price) > { > ... > } > } > > Regards, > Tim > > > On 12 Jan 2017, at 22:39, Fleshgrinder wrote: > > > > On 1/12/2017 10:13 PM, Marco Pivetta wrote: > >> Hey Richard, > >> > >> I made an example where everything was in a single class, but most > >> scenarios involve a lazy-loading wrapper that has no knowledge of the > >> original class besides its constructor. > >> > >> I use this approach to generate proxy classes that are "safe" to use > with > >> fluent interfaces, for example (because fluent interfaces are really a > mess > >> to work with). > >> > >> The alternative (for me, specifically) is to copy the constructor AST > into > >> a closure, then store the closure somewhere. Still, if that closure also > >> calls the private constructor, I have to do it recursively, and so on > until > >> I just give up :-P Also, this assumes codegen. Large pieces of code will > >> need rewriting, whereas I don't see strong reasoning for dropping a > feature > >> that, while weird, is actually useful. > >> > >> Marco Pivetta > >> > > > > That actually calls out for reflection and that's what it is good for. > > Even if explicit construct calls are to be forbidden, the reflection API > > is definitely a different story. > > > > That being said, your remarks definitely tell everyone that a change in > > this area -- if wanted -- is only possible in PHP 8 and has to be > > considered a breaking change. > > > > ``` > > > > > final class DbConnection { > > > > private $dsn; > > > > private $initializer; > > > > public function __construct($dsn) { > > $this->dsn = $dsn; > > // socket stuff happens here, much like with PDO > > } > > > > public static function lazyInstance($dsn) { > > $reflector = new ReflectionClass(self::class); > > $self = $reflector->newInstanceWithoutConstructor(); > > $new = $reflector->getConstructor(); > > > > $self->initializer = function () use ($self, $new, $dsn) { > > $new->invoke($self, $dsn); > > $self->initializer = function () {}; > > }; > > > > return $self; > > } > > > > public function query($queryString) { > > $this->initializer->__invoke(); > > > > // irrelevant from here on > > return ['query' => $queryString, 'dsn' => $this->dsn]; > > } > > > > } > > > > $instance = DbConnection::lazyInstance('mysql://something'); > > > > var_dump($instance); > > > > var_dump($instance->query('SELECT * FROM foo')); > > var_dump($instance->query('SELECT * FROM bar')); > > ``` > > > > On 1/12/2017 10:34 PM, Tim Bezhashvyly wrote: > >> 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. > >> > > > > I completely share this concern with Tim. Of course reflection would > > still allow one to circumvent any limitations imposed by the normal > > runtime. But that's what reflection is for after all. > > > > On 1/12/2017 10:34 PM, Tim Bezhashvyly wrote: > >> And what about polymorphic dispatch? > >> > > > > I think you need to elaborate a bit more here to get some feedback. > > > > -- > > Richard "Fleshgrinder" Fussenegger > > That's generally understood as "Method overloading". There were more discussions about it in here recently: you might want to look them up. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --f403045f5580d067440545ecbca5--