Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66677 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72936 invoked from network); 18 Mar 2013 05:52:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2013 05:52:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=pierre@pcservice.co.za; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=pierre@pcservice.co.za; sender-id=pass Received-SPF: pass (pb1.pair.com: domain pcservice.co.za designates 69.41.247.18 as permitted sender) X-PHP-List-Original-Sender: pierre@pcservice.co.za X-Host-Fingerprint: 69.41.247.18 gateway08.websitewelcome.com Linux 2.6 Received: from [69.41.247.18] ([69.41.247.18:57489] helo=gateway08.websitewelcome.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C6/F1-57457-B9BA6415 for ; Mon, 18 Mar 2013 00:52:29 -0500 Received: by gateway08.websitewelcome.com (Postfix, from userid 5007) id 9A2ABA9DF3FAC; Mon, 18 Mar 2013 00:52:24 -0500 (CDT) Received: from vinacomin.websitewelcome.com (vinacomin.websitewelcome.com [50.97.101.199]) by gateway08.websitewelcome.com (Postfix) with ESMTP id 8E038A9DF3F87 for ; Mon, 18 Mar 2013 00:52:24 -0500 (CDT) Received: from [209.85.128.51] (port=65155 helo=mail-qe0-f51.google.com) by vinacomin.websitewelcome.com with esmtpsa (TLSv1:RC4-SHA:128) (Exim 4.80) (envelope-from ) id 1UHSzg-0007B4-AP for internals@lists.php.net; Mon, 18 Mar 2013 00:52:24 -0500 Received: by mail-qe0-f51.google.com with SMTP id nd7so3145938qeb.10 for ; Sun, 17 Mar 2013 22:52:23 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.49.97.134 with SMTP id ea6mr20352544qeb.33.1363585943652; Sun, 17 Mar 2013 22:52:23 -0700 (PDT) Reply-To: pierre@pcservice.co.za Received: by 10.49.129.134 with HTTP; Sun, 17 Mar 2013 22:52:23 -0700 (PDT) Received: by 10.49.129.134 with HTTP; Sun, 17 Mar 2013 22:52:23 -0700 (PDT) In-Reply-To: References: Date: Mon, 18 Mar 2013 07:52:23 +0200 Message-ID: To: Thomas Bley Cc: PHP Development , Daniele Orlando Content-Type: multipart/alternative; boundary=047d7bd766c4cae3d304d82c9775 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vinacomin.websitewelcome.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - pcservice.co.za X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (mail-qe0-f51.google.com) [209.85.128.51]:65155 X-Source-Auth: pierre@pcservice.co.za X-Email-Count: 3 X-Source-Cap: cGNzZXJ2aWM7cGllcnJlO3ZpbmFjb21pbi53ZWJzaXRld2VsY29tZS5jb20= Subject: Re: [PHP-DEV] __invokeStatic() method From: pierre@pcservice.co.za (Pierre du Plessis) --047d7bd766c4cae3d304d82c9775 Content-Type: text/plain; charset=ISO-8859-1 On Mar 18, 2013 2:41 AM, "Thomas Bley" wrote: > > On Sat, Mar 16, 2013 at 9:33 PM, Pierre du Plessis > wrote: > > On Mar 16, 2013 9:35 PM, "Daniele Orlando" wrote: > >> > >> Hi List, > >> > >> I'm interested in proposing an RFC and I would know your opinion. > >> > >> === Current Situation === > >> Since PHP 5.3 we can use an object instance, who defines the __invoke() > >> method, as a callable object. > >> Example: > >> > >> // PHP Code. > >> class Runnable > >> { > >> public function __invoke() > >> { > >> echo "Runned"; > >> } > >> } > >> > >> $r = new Runnable(); > >> $r(); > >> > >> // Output > >> Runned > >> > >> === The Idea === > >> 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: > >> > >> // Python Code. > >> class A: > >> pass > >> > >> A() > >> > >> // Output. > >> <__main__.A instance at %address> > >> > >> 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: > >> > >> // PHP Code. > >> class TrueRunnable > >> { > >> public static function __invokeStatic() > >> { > >> echo "Runned"; > >> } > >> } > >> > >> TrueRunnable(); > >> > >> // Output. > >> Runned > >> > >> But the possibility are endless: > >> > >> class A > >> { > >> public static function __invokeStatic() > >> { > >> return new A(); > >> } > >> public method m() {} > >> } > >> > >> A()->m(); > >> > >> // or > >> > >> class A > >> { > >> private $_instance; > >> public static function __invokeStatic() > >> { > >> // Singleton pattern. > >> if (self::$_instance) { > >> return self::$_instance; > >> } > >> > >> return self::$_instance = new A(); > >> } > >> public method m() {} > >> } > >> > >> A()->m(); > >> > >> > >> === Conclusion === > >> This feature makes the __invoke() method consistent with the __call() and > >> __callStatic() methods, > >> and opens the door to many cool stuff. > >> > >> Any feedback is appreciated. > >> > >> Daniele Orlando > > > > I don't really see a use case for this, as you can already use the syntax > > A::method(); > > > > E.G class A { public static function invoke() { return new A; } > > > > public function m() { echo 'Runned'; } > > > > A::invoke()->m(); > > > > 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. > > 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. > > Regards, > Thomas > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > Using A() looks too much like a function call. And there is still the issue with having a function name the same as the class name. --047d7bd766c4cae3d304d82c9775--