Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97736 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36919 invoked from network); 12 Jan 2017 21:51:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2017 21:51:18 -0000 Authentication-Results: pb1.pair.com header.from=tim.bezhashvyly@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tim.bezhashvyly@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.66 as permitted sender) X-PHP-List-Original-Sender: tim.bezhashvyly@gmail.com X-Host-Fingerprint: 74.125.82.66 mail-wm0-f66.google.com Received: from [74.125.82.66] ([74.125.82.66:33983] helo=mail-wm0-f66.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 65/9B-50165-55AF7785 for ; Thu, 12 Jan 2017 16:51:17 -0500 Received: by mail-wm0-f66.google.com with SMTP id c85so6858322wmi.1 for ; Thu, 12 Jan 2017 13:51:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=C+2MUjvZRvUiSVX4wtLt7RkZgXcMarlWJf1vSZvbySc=; b=BuNPS8KnjRGPXjEn0t5mSiZn4rzUWklE7dNrTcUCQEIzN5d/flrNR8WgYJmh3/2d19 sJremiF95OVLnRq6x4pDuuUoSFgvUpdGm0xlmz12+k3MtiuVeI8yHRxjjVjmJ8whWVZf 6iyCsw10v48YO/Ga584lsW4HhweK3w9ZPD5eRrmhzdJa3oKmAXp5U9t7+K0+E+JbfmiD hAlVJab8TNd3vsa6orfVaC1nqQ8HT0RfBt3Tvne1SfkgZRA0gZ1cxsBq86Wv/J2GCiY3 2ChG7uJNTBeXmVgMsHVwKJOyYUWdm41K+bEKRSZOUAF7qf1PAp7/jJgNusYrlb3IHIwe +Zbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=C+2MUjvZRvUiSVX4wtLt7RkZgXcMarlWJf1vSZvbySc=; b=O1UZtnKyymbPLlCB79BtnsbXzzPOASOS+2LVES9TaZtjy8uXkoyi1e+C7mMt2o0sFN xeUWMVOF+91sAYNtXXq/PO7t5NzYuCx9WLSCYU3HEQrwA0PqTFo7/cUjGyuOKkn2mUui 7Bs4H0B6fQKWHlFC9eKbpEMevXmRK8gOnxLaYX7V3E2vOm5g6d6a4anD+BzJLds3CSRS MP2GrWBUoSY4UGjXazSBPRndjL9JRu/yygTJ3tN5OwkUH+akO2h/iy9VN/05iLqMT2BL Z1Slsp/jl/PuJn8Offtia/XOG6iwU1LHJuoIGaKuqXY0wiDV6U1WwEslYcRKH7Rxx5ai taCA== X-Gm-Message-State: AIkVDXJy9Ia43FJm1IBosRAAaIRlKjFwi/HLHX2Jj7BaQDvrYCt8g2NaN7QFC/3gjcXmOQ== X-Received: by 10.28.158.196 with SMTP id h187mr157447wme.59.1484257874502; Thu, 12 Jan 2017 13:51:14 -0800 (PST) Received: from ?IPv6:2003:d0:3c1:e800:7dca:f5b9:d95a:e519? (p200300D003C1E8007DCAF5B9D95AE519.dip0.t-ipconnect.de. [2003:d0:3c1:e800:7dca:f5b9:d95a:e519]) by smtp.gmail.com with ESMTPSA id x135sm5735001wme.23.2017.01.12.13.51.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2017 13:51:13 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.2 \(3259\)) In-Reply-To: Date: Thu, 12 Jan 2017 22:51:12 +0100 Cc: Marco Pivetta Content-Transfer-Encoding: quoted-printable Message-ID: <910A374A-8C98-4F60-918D-CF0012FB7518@gmail.com> References: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> To: internals@lists.php.net X-Mailer: Apple Mail (2.3259) Subject: Re: [PHP-DEV] Explicit constructor call and polymorphic dispatch From: tim.bezhashvyly@gmail.com (Tim Bezhashvyly) 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: >=20 > On 1/12/2017 10:13 PM, Marco Pivetta wrote: >> Hey Richard, >>=20 >> 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. >>=20 >> 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). >>=20 >> 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. >>=20 >> Marco Pivetta >>=20 >=20 > 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. >=20 > 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. >=20 > ``` > =20 > final class DbConnection { >=20 > private $dsn; >=20 > private $initializer; >=20 > public function __construct($dsn) { > $this->dsn =3D $dsn; > // socket stuff happens here, much like with PDO > } >=20 > public static function lazyInstance($dsn) { > $reflector =3D new ReflectionClass(self::class); > $self =3D $reflector->newInstanceWithoutConstructor(); > $new =3D $reflector->getConstructor(); >=20 > $self->initializer =3D function () use ($self, $new, $dsn) { > $new->invoke($self, $dsn); > $self->initializer =3D function () {}; > }; >=20 > return $self; > } >=20 > public function query($queryString) { > $this->initializer->__invoke(); >=20 > // irrelevant from here on > return ['query' =3D> $queryString, 'dsn' =3D> $this->dsn]; > } >=20 > } >=20 > $instance =3D DbConnection::lazyInstance('mysql://something'); >=20 > var_dump($instance); >=20 > var_dump($instance->query('SELECT * FROM foo')); > var_dump($instance->query('SELECT * FROM bar')); > ``` >=20 > 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. >>=20 >=20 > 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. >=20 > On 1/12/2017 10:34 PM, Tim Bezhashvyly wrote: >> And what about polymorphic dispatch? >>=20 >=20 > I think you need to elaborate a bit more here to get some feedback. >=20 > --=20 > Richard "Fleshgrinder" Fussenegger