Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91440 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3905 invoked from network); 26 Feb 2016 17:08:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Feb 2016 17:08:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=danack@basereality.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=danack@basereality.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain basereality.com from 209.85.161.175 cause and error) X-PHP-List-Original-Sender: danack@basereality.com X-Host-Fingerprint: 209.85.161.175 mail-yw0-f175.google.com Received: from [209.85.161.175] ([209.85.161.175:34061] helo=mail-yw0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B6/E5-55238-98680D65 for ; Fri, 26 Feb 2016 12:08:26 -0500 Received: by mail-yw0-f175.google.com with SMTP id h129so73923773ywb.1 for ; Fri, 26 Feb 2016 09:08:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=LQFzzDJn7Xrxv0ewiRxXuhuSgYeMg1hZ61Ej1fIT4Rs=; b=ukyy5epf3ESu127tS2Txutjy84BebhZ+GS6BsyCQ/Puu70abbjBn8g6//4LKkZaMMy mJnP2JnHSUiFCXb0ATIodJjU4cP2/Fi8iqLQECKHEUI0ik8/QGf2bm6pTzXe+2m9oxz4 uChTxDxFQTDlEcp+iEdp+TNiaizENpUMe7KOewM9iEGJt8RwiuhhXUw+3GiFXJqTpTGq aNF2DTUoyKCFgJY9NuD9pJL1Sm9bA6rYVL9P9tV3Rl/Qm8gmV/ng0l/vW4oO8Xqx283z XTWoyj+c3jPLACl2Vl/DA0rhyDTeZ1HvohAHdIL07WjWQJjZpTOJzLMDxdhEErMGtTi3 4H7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=LQFzzDJn7Xrxv0ewiRxXuhuSgYeMg1hZ61Ej1fIT4Rs=; b=Ai7Y9TXJXq6RwEq5IxDMmzrovEJb6e5oP6bEQDAjrgqU+WJ5Q1AmA/xQrcdAM4aLd+ qF91b6vaKvdIe3q62dBeEccUC+sVQaNBC3PvldZXp41ipAJWZ7g1GyN3l0I/gd0mRcq4 DmTPdb+wh/WnEhI/PNlOSNA/UjR6DdF41nJittTJf4Y3e/Gc0m9NwuEiihRy+OxYiCyJ tFOwDRUPjkg4Lj+6W+jaE+zlUaDmHQ7ilIacQZgGA1EHzAgakLAkBtdLa1gCoQ+ZC03b H8dSI988skZSO7qpDiaWwBCtSVAXKWqivL534Nl2bixshVzi00VzoWuJiap6C/h8US0x +t+w== X-Gm-Message-State: AD7BkJLNauxvzk4l4qm0GoWkNkVWsj/Ia1nsTvXyxG11ED9UzD0SEkwQl2oQnkK9XrkgYemXzCe26TEN7goRJg== MIME-Version: 1.0 X-Received: by 10.13.192.5 with SMTP id b5mr1496640ywd.114.1456506502057; Fri, 26 Feb 2016 09:08:22 -0800 (PST) Received: by 10.37.43.85 with HTTP; Fri, 26 Feb 2016 09:08:21 -0800 (PST) X-Originating-IP: [2.98.210.180] In-Reply-To: References: Date: Fri, 26 Feb 2016 17:08:21 +0000 Message-ID: To: Adam Harvey , Rowan Collins Cc: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [RFC] Callable constructors From: danack@basereality.com (Dan Ackroyd) On 25 February 2016 at 22:43, Adam Harvey wrote: > Why is having a special syntax that is considered callable and > conflates instantiation and method calls better than having the injector accept a class name > and use reflection as required to instantiate that? > They're > trivially special cased without muddying instantiation and method > calls. Hence my "are there analogues for this elsewhere" question. I think this is actually something that would be useful across applications - not just my own library. Although I could use the Reflection code that Johannes wrote to make it possible for my own code to use constructors as a callable, I wouldn't be able to use it in anyone else's code, unless I asked them nicely to add the special casing to their library code as well. For example I would hope to use callable constructors with the Zend service manager. That is a good example of where just being able to specify "Foo::__construct" in a config file would save having to write 'boiler plate' instantiation functions for every object. I think there is a small 'chicken and egg' problem here. Of course no one is currently using constructors as callable, because it isn't possible yet, and so it's hard to point to any code that uses them. And so it appears that it's an small use case. I'm pretty sure if it was adopted it would become a standard thing to do when 'wiring up' applications, or hydrating objects fetched from various data sources. Rowan Wrote: > I would like to join Adam in asking if > you can find examples in other languages where constructors can be used > as though they were static methods in this way. Python would be a good example. constructors are just functions that can be called, and passed around as 'callable' things. Obviously Pythons concepts of classes aren't identical to PHP' concepts - but the code is doing the same thing; passing around a thing that can be called without having to use Reflection to make it a callable. http://goo.gl/SnTNZH ==== Begin from random import randint class Foo: def f(self): return 'I am foo' class Bar: def f(self): return 'I am bar' if randint(0,1) == 0: classname = Foo else: classname = Bar obj = classname() print obj.f() ==== End I'm not aware of any languages that contemplated making it be callable, but then decided it would be a bad idea. cheers Dan