Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97730 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25223 invoked from network); 12 Jan 2017 20:36:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2017 20:36:09 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.41 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.41 mail-wm0-f41.google.com Received: from [74.125.82.41] ([74.125.82.41:37805] helo=mail-wm0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 12/49-50165-7B8E7785 for ; Thu, 12 Jan 2017 15:36:08 -0500 Received: by mail-wm0-f41.google.com with SMTP id c206so39164745wme.0 for ; Thu, 12 Jan 2017 12:36:07 -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=ZXEqyp76ouFQVITqLbVqEeoD9X/bnqra+JK/gXX0l9k=; b=OC9Uixj+cBSCEuT9afi6p+5N7wu0+4TPF/hBDpTDktR5YuXWotgn+sJ9G0MTRixghS e4kC/5SQ+yM0vozIB+JUaV5279SsaWSLnb/ne0unHlZgidrvyCAVe9prSAZKeLq8I2Qz /CoG8WSqJAGAHWZ9b/Qy4A/T3qfmxee5pkx0MBr6IoboeNnLvBl0szWsAJdu/S1QGygP 9UMdgyDwTkDEn6GswNJXp3SjqEK+a7nlvpc7HKCfGdOieODftil95yfMvEtPIdjYYo/Z DTuuttN+k+WPmN+3jiVxaQLf7IT7YxGR3t51Azg4ikh4ZYgc6i2s1McMXB3vtY2WFSo4 P9aQ== 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=ZXEqyp76ouFQVITqLbVqEeoD9X/bnqra+JK/gXX0l9k=; b=ngkfpm3i4JzBWPytG/iDuFRefyP49hTKgcL9jsyHB6rf7JhMT27cxLyHdlOn3TDsV+ TfH0yydxEpH/y92tg57kD0KRf79AlewG+f+nb4eDE4MLU15CBQXjyFVYnm6JBVemN8/Z E+U7Lhkmub1qPv+06yAf2R4shn49xEJFlsf7drSuzpH6Nli//Op0a01fSjiDYE86vdX1 ucYHVAft5NSJpS4t/DySYh6/RbllOn1TZsZR2DZ7VbG/p/mCHxDRYyG+eVS95Py7izN+ A7Fcuj5R5X+9a+e3CV3FV5WoP1wGJasRxCuYcMQV0+BgPjLjS6b7YHNSUxmB4VmDF0z8 oa0w== X-Gm-Message-State: AIkVDXIKbqn3pAok9b0XF71riua39M7Af2ru+wLWyLEHhwGy/xHW67XKsPBfqms0XYIa0GCSWmE8yVcnbMg2tA== X-Received: by 10.223.154.132 with SMTP id a4mr9802769wrc.188.1484253364352; Thu, 12 Jan 2017 12:36:04 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.34.197 with HTTP; Thu, 12 Jan 2017 12:35:43 -0800 (PST) In-Reply-To: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> References: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> Date: Thu, 12 Jan 2017 21:35:43 +0100 Message-ID: To: Tim Bezhashvyly Cc: PHP Internals Content-Type: multipart/alternative; boundary=f403045f558089fc5e0545ebaa6d Subject: Re: [PHP-DEV] Explicit constructor call and polymorphic dispatch From: ocramius@gmail.com (Marco Pivetta) --f403045f558089fc5e0545ebaa6d Content-Type: text/plain; charset=UTF-8 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: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 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://ocramius.github.com/ On Thu, Jan 12, 2017 at 8:11 AM, Tim Bezhashvyly 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 > > --f403045f558089fc5e0545ebaa6d--