Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97732 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28960 invoked from network); 12 Jan 2017 20:59:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2017 20:59:03 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 212.232.28.122 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 212.232.28.122 mx201.easyname.com Received: from [212.232.28.122] ([212.232.28.122:52914] helo=mx201.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 34/0A-50165-31EE7785 for ; Thu, 12 Jan 2017 15:59:01 -0500 Received: from cable-81-173-135-7.netcologne.de ([81.173.135.7] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1cRmSX-0000L1-H6; Thu, 12 Jan 2017 20:58:57 +0000 Reply-To: internals@lists.php.net References: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> To: Marco Pivetta , Tim Bezhashvyly Cc: PHP Internals Message-ID: Date: Thu, 12 Jan 2017 21:58:53 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-DNSBL-PBLSPAMHAUS: YES Subject: Re: [PHP-DEV] Explicit constructor call and polymorphic dispatch From: php@fleshgrinder.com (Fleshgrinder) On 1/12/2017 9:35 PM, Marco Pivetta 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: > 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 > > 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 > Very creative but why not the following? ``` dsn = $dsn; } private function init() { $this->initialized = true; echo $this->dsn , "\n"; } public function query($queryString) { // this is actually cheaper than calling the closure $this->initialized || $this->init(); return ['query' => $queryString, 'dsn' => $this->dsn]; } } $db_conn = new DbConnection('mysql://something'); var_dump( $db_conn, $db_conn->query('SELECT * FROM foo'), $db_conn->query('SELECT * FROM bar') ); ``` https://3v4l.org/PaqqZ Works equally well in PHP and HHVM and achieves your goal without using undocumented side effects and reflection. Adding the ability of a non-lazy and lazy construction is easily added too if desired. -- Richard "Fleshgrinder" Fussenegger