Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97734 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33322 invoked from network); 12 Jan 2017 21:34:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2017 21:34:51 -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.50 as permitted sender) X-PHP-List-Original-Sender: tim.bezhashvyly@gmail.com X-Host-Fingerprint: 74.125.82.50 mail-wm0-f50.google.com Received: from [74.125.82.50] ([74.125.82.50:37235] helo=mail-wm0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 73/DA-50165-976F7785 for ; Thu, 12 Jan 2017 16:34:50 -0500 Received: by mail-wm0-f50.google.com with SMTP id c206so41441964wme.0 for ; Thu, 12 Jan 2017 13:34:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=WQMvLGEUqS6+sO/JCJZPDcea8EyNTDcEk+DOHTFdeRM=; b=DHD5FgJ1T6uEt2ymXvLYib0ANt4NjcXc55x/4gLMcEJM+6dOGaNhCoqzKFvo/JKNgo kPCQpkBrEdZDD3dMDgud9qmpCOC7uKNKjD1MJ0TBM5E30VCvCjBasHPAhaCMZHlXJhwY N71oIqEuniAycqt+V81ED54UurkNdJzQVf+j3qnmTjZWn2CJx7WmtCZGoMFVmj7hBrgJ R2GkmeFm34a3mk+H5hCr9hp1bn0C+ZHQ61BA2nLnCDKLJdrW0t0JTycp17lWT4vBpMbd 7zXj0yDPZeG4Pyd8iK8o+9acMCTYiovL2ooWwzu6/oXVK5jm0KETzYIUCQErp3xkDlm1 3Mrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=WQMvLGEUqS6+sO/JCJZPDcea8EyNTDcEk+DOHTFdeRM=; b=nt1b/luHj9faUwu5u3qn8Cdgkvqt4RSilFwGEs7YqVtARMr9rG9oeT9O5brarlpcn+ 59bTSSc3TilrcCMl6iVVLGeIga/FGrQfVxCe1pXfThXHUbALEU8huuMg/6JG/wl9QABF X12M3rWayDFfWXMtg0Vqcx3EiX5iiYa8yumBVH+1qMQWvOFFyICLpNX7xMnFX+TzO9UE 60a/q/NxkmkrTsMX9XSQVzRDURidkp671nlEhLlYmb21FqIBDbxn+bJ71QZ95OpMzsTD ON/G2LjGzUJx7hIPv4pEHn4Z2CAK+l2OADmE6Hq2b76mdzE1f3z+avAAPy/Ue//1ZTF/ RxSQ== X-Gm-Message-State: AIkVDXJ/jtCBw7y51nL0NE/L7iCQHvJODwRDDP1nPJewtBgYCU12lH5ee/PpNzf8IpwoNQ== X-Received: by 10.28.39.134 with SMTP id n128mr110464wmn.27.1484256886908; Thu, 12 Jan 2017 13:34:46 -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 x140sm5638148wme.19.2017.01.12.13.34.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2017 13:34:46 -0800 (PST) Message-ID: <9DC465CB-ABE5-4245-A228-0FB332EF1152@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_B5B1D40A-E86C-4BE6-9F1C-CFC57715D23C" Mime-Version: 1.0 (Mac OS X Mail 10.2 \(3259\)) Date: Thu, 12 Jan 2017 22:34:44 +0100 In-Reply-To: Cc: PHP Internals To: Marco Pivetta References: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> X-Mailer: Apple Mail (2.3259) Subject: Re: [PHP-DEV] Explicit constructor call and polymorphic dispatch From: tim.bezhashvyly@gmail.com (Tim Bezhashvyly) --Apple-Mail=_B5B1D40A-E86C-4BE6-9F1C-CFC57715D23C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 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 wrote: >=20 > Heya, >=20 > While I agree that it is weird to be able to call constructors more = than once, this is generally used for: >=20 > * lazy loading > * resource reset >=20 > Specifically, what is going on is something like following:=20 > final class DbConnection > { > private $dsn; > private $initializer; > public function __construct(string $dsn) > { > $this->dsn =3D $dsn; > // socket stuff happens here, much like with PDO > } >=20 > public function query(string $queryString) : array > { > ($this->initializer)(); > // irrelevant from here on > return ['query' =3D> $queryString, 'dsn' =3D> $this->dsn]; > } >=20 > public static function lazyInstance(string $dsn) : self > { > $instance =3D (new = ReflectionClass(self::class))->newInstanceWithoutConstructor(); > $instance->initializer =3D function () use ($dsn, $instance) { > $instance->__construct($dsn); > $instance->initializer =3D function () { > }; > }; > return $instance; > } > } >=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 > Here's an example of it at work: https://3v4l.org/Y0eoL = >=20 > The pattern is simple: >=20 > * intercept constructor call > * capture constructor parameters > * instantiate without constructor > * defer constructor call for later >=20 > 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). >=20 > Therefore I don't see a reason to drop manual constructor calls, = unless there is a strong necessity to get rid of 'em. >=20 >=20 >=20 > Marco Pivetta=20 >=20 > http://twitter.com/Ocramius =20 >=20 > http://ocramius.github.com/ >=20 > On Thu, Jan 12, 2017 at 8:11 AM, Tim Bezhashvyly = > wrote: > Dear internals, >=20 > I would like to propose 2 RFCs: >=20 > - Disallow explicit call of __construct method > - Polymorphic dispatch >=20 > 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. >=20 > Otherwise please bless me with mana which will allow me to submit = them. >=20 > Regards, > Tim > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php = >=20 >=20 --Apple-Mail=_B5B1D40A-E86C-4BE6-9F1C-CFC57715D23C--