Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66678 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77331 invoked from network); 18 Mar 2013 07:07:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2013 07:07:50 -0000 Authentication-Results: pb1.pair.com smtp.mail=mrtreinis@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mrtreinis@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.169 as permitted sender) X-PHP-List-Original-Sender: mrtreinis@gmail.com X-Host-Fingerprint: 209.85.217.169 mail-lb0-f169.google.com Received: from [209.85.217.169] ([209.85.217.169:35469] helo=mail-lb0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A6/82-57457-34DB6415 for ; Mon, 18 Mar 2013 02:07:48 -0500 Received: by mail-lb0-f169.google.com with SMTP id m4so4393440lbo.0 for ; Mon, 18 Mar 2013 00:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=z6UHELvC9oyrVGOTFvdUqRsyEI7GdUtUL01m3gaGdGs=; b=flsRUQXkysFdkR9OUoMfN62b9v2f+TjzV+pu/RUVriy1JnSyBWkJlaMqMP5t9MdGCr MFVrXeeQPjYmNaD55CRnQ8u3MGNbEfCqw8y2qHl8sGa87bsDl50w3udrkzSaMNS4aw28 LfpCYpQDTIH/RuW2hvMrz/8Yy66a85+BhelfePzOJik6UCsfIH20nB4aDCS6IfC593KI B1LPM695X7XN9XdeM7AkmtOo3g1eQ9qU3MgLewxNcO2ARBFooWcnRd4vSZXUf3Am7Qwk xl5qythz75WeCEtnbSI8rV+wDcArYLEEVwI/V8SaOkltp6BqrKVZCq+Iypc4gNvBBHV9 KFPQ== MIME-Version: 1.0 X-Received: by 10.152.104.80 with SMTP id gc16mr13514222lab.49.1363590463994; Mon, 18 Mar 2013 00:07:43 -0700 (PDT) Received: by 10.114.14.131 with HTTP; Mon, 18 Mar 2013 00:07:43 -0700 (PDT) In-Reply-To: References: Date: Mon, 18 Mar 2013 09:07:43 +0200 Message-ID: To: pierre@pcservice.co.za Cc: Thomas Bley , PHP Development , Daniele Orlando Content-Type: multipart/alternative; boundary=f46d040714d539c0a904d82da547 Subject: Re: [PHP-DEV] __invokeStatic() method From: mrtreinis@gmail.com (=?UTF-8?Q?Mat=C4=ABss_Roberts_Treinis?=) --f46d040714d539c0a904d82da547 Content-Type: text/plain; charset=UTF-8 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. $name = 'something'; $something = $name(); // What is this - a function call or object? Fact is, we can not know that unless a) do a search in code b) var_dump? 2013/3/18 Pierre du Plessis > 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. > --f46d040714d539c0a904d82da547--