Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66679 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80556 invoked from network); 18 Mar 2013 07:49:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2013 07:49:36 -0000 Authentication-Results: pb1.pair.com header.from=davidkmuir@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=davidkmuir@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.169 as permitted sender) X-PHP-List-Original-Sender: davidkmuir@gmail.com X-Host-Fingerprint: 209.85.192.169 mail-pd0-f169.google.com Received: from [209.85.192.169] ([209.85.192.169:43997] helo=mail-pd0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 12/03-57457-F07C6415 for ; Mon, 18 Mar 2013 02:49:36 -0500 Received: by mail-pd0-f169.google.com with SMTP id 3so755752pdj.28 for ; Mon, 18 Mar 2013 00:49:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:references:mime-version:in-reply-to:content-type :content-transfer-encoding:message-id:cc:x-mailer:from:subject:date :to; bh=Mls6umggeRhxjTQiuCcfXGYPDIQBxnzQQTHK39dqB7k=; b=COeiYBCl7mV/6LuO8yit0ULzz3pmP7qZyCciFQHf1Nd8WtTmfzqooGEaEvYWxnM6VR aNBvkDSK1FYLh81a7Aiuw0HHdeTeT0doUQAizvuqVKCAW9xdcjA67RKDWSsk0TSRHP0L 4zhS0cFufHRe8l3qHOzfiKSIoCf1qdhCNWX6NQuR5TgQt2z66aTBeDbc6gAwEp+Cr4R5 22lKch8eCiJaOsnseOcrqXOvr8MCNSkQyxij34SPdEj2rPBE7QwoihTPiKkrTYi8Y6sI Rx3swNvO6Nsak1CP/TpT5+Jv/SQLJGAicRfRTUEL4riNZVrk2JaOEyk++35U46FlHEFt fBEQ== X-Received: by 10.66.119.7 with SMTP id kq7mr9012109pab.116.1363592973198; Mon, 18 Mar 2013 00:49:33 -0700 (PDT) Received: from [192.168.0.5] (115-64-165-88.static.tpgi.com.au. [115.64.165.88]) by mx.google.com with ESMTPS id 4sm19297937pbn.23.2013.03.18.00.49.29 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 18 Mar 2013 00:49:32 -0700 (PDT) References: Mime-Version: 1.0 (1.0) In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Message-ID: <4DAE13E9-5C10-42DC-A7BE-48A54B734520@gmail.com> Cc: "pierre@pcservice.co.za" , Thomas Bley , PHP Development , Daniele Orlando X-Mailer: iPhone Mail (10B146) Date: Mon, 18 Mar 2013 18:49:26 +1100 To: =?utf-8?Q?Mat=C4=ABss_Roberts_Treinis?= Subject: Re: [PHP-DEV] __invokeStatic() method From: davidkmuir@gmail.com (David Muir) On 18/03/2013, at 6:07 PM, Mat=C4=ABss Roberts Treinis = wrote: > Not only that. This potentially might break compatibility with many > software products already out there. Also, this might lead to many > misunderstandings and, in fact, ambiguous code. Consider the sample. >=20 > $name =3D 'something'; > $something =3D $name(); // What is this - a function call or object? >=20 > Fact is, we can not know that unless a) do a search in code b) var_dump? >=20 >=20 > 2013/3/18 Pierre du Plessis >=20 >> On Mar 18, 2013 2:41 AM, "Thomas Bley" wrote: >>>=20 >>> On Sat, Mar 16, 2013 at 9:33 PM, Pierre du Plessis >>> wrote: >>>> On Mar 16, 2013 9:35 PM, "Daniele Orlando" wrote: >>>>>=20 >>>>> Hi List, >>>>>=20 >>>>> I'm interested in proposing an RFC and I would know your opinion. >>>>>=20 >>>>> =3D=3D=3D Current Situation =3D=3D=3D >>>>> Since PHP 5.3 we can use an object instance, who defines the >> __invoke() >>>>> method, as a callable object. >>>>> Example: >>>>>=20 >>>>> // PHP Code. >>>>> class Runnable >>>>> { >>>>> public function __invoke() >>>>> { >>>>> echo "Runned"; >>>>> } >>>>> } >>>>>=20 >>>>> $r =3D new Runnable(); >>>>> $r(); >>>>>=20 >>>>> // Output >>>>> Runned >>>>>=20 >>>>> =3D=3D=3D The Idea =3D=3D=3D >>>>> In Python, when you construct an object, you don't need to use the >> "new" >>>>> keyword but you just invoke the class name followed by "()", like the >>>> class >>>>> is a function. >>>>> Example: >>>>>=20 >>>>> // Python Code. >>>>> class A: >>>>> pass >>>>>=20 >>>>> A() >>>>>=20 >>>>> // Output. >>>>> <__main__.A instance at %address> >>>>>=20 >>>>> Now, would be interesting to extend the PHP __invoke() method adding >> an >>>>> __invokeStatic() method, like happens with __call() and __callStatic()= >>>>> methods. >>>>> In this way could be possible to use a class name to invoke the >>>>> __invokeStatic() method. >>>>> Example: >>>>>=20 >>>>> // PHP Code. >>>>> class TrueRunnable >>>>> { >>>>> public static function __invokeStatic() >>>>> { >>>>> echo "Runned"; >>>>> } >>>>> } >>>>>=20 >>>>> TrueRunnable(); >>>>>=20 >>>>> // Output. >>>>> Runned >>>>>=20 >>>>> But the possibility are endless: >>>>>=20 >>>>> class A >>>>> { >>>>> public static function __invokeStatic() >>>>> { >>>>> return new A(); >>>>> } >>>>> public method m() {} >>>>> } >>>>>=20 >>>>> A()->m(); >>>>>=20 >>>>> // or >>>>>=20 >>>>> class A >>>>> { >>>>> private $_instance; >>>>> public static function __invokeStatic() >>>>> { >>>>> // Singleton pattern. >>>>> if (self::$_instance) { >>>>> return self::$_instance; >>>>> } >>>>>=20 >>>>> return self::$_instance =3D new A(); >>>>> } >>>>> public method m() {} >>>>> } >>>>>=20 >>>>> A()->m(); >>>>>=20 >>>>>=20 >>>>> =3D=3D=3D Conclusion =3D=3D=3D >>>>> This feature makes the __invoke() method consistent with the __call() >> and >>>>> __callStatic() methods, >>>>> and opens the door to many cool stuff. >>>>>=20 >>>>> Any feedback is appreciated. >>>>>=20 >>>>> Daniele Orlando >>>>=20 >>>> I don't really see a use case for this, as you can already use the >> syntax >>>> A::method(); >>>>=20 >>>> E.G class A { public static function invoke() { return new A; } >>>>=20 >>>> public function m() { echo 'Runned'; } >>>>=20 >>>> A::invoke()->m(); >>>>=20 >>>> Your example above only saves a few characters to type and can lead to >> a >>>> lot of problems if you have a function with the same name as the class.= >>>=20 >>> Using A::invoke(), you need to know the name of "invoke()" and it's >>> hard to force users always to use the invoke() function. >>> Using A() would be more clean since all the static init(), factory(), >>> invoke(), getInstance() are gone. >>> Having __call(), __callStatic(), __invoke() and invokeStatic() would >>> make the overloading concept more consistent. >>>=20 >>> Regards, >>> Thomas >>>=20 >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >>=20 >> Using A() looks too much like a function call. And there is still the iss= ue >> with having a function name the same as the class name. >>=20 This works well in languages like JavaScript where functions are objects. As much as I like the feature there, I'm not sure it's a great fit for PHP. David Sent from my iPhone=