Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66676 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58287 invoked from network); 18 Mar 2013 00:40:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2013 00:40:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=thbley@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=thbley@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.51 as permitted sender) X-PHP-List-Original-Sender: thbley@gmail.com X-Host-Fingerprint: 209.85.210.51 mail-da0-f51.google.com Received: from [209.85.210.51] ([209.85.210.51:63382] helo=mail-da0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 76/10-57457-77266415 for ; Sun, 17 Mar 2013 19:40:23 -0500 Received: by mail-da0-f51.google.com with SMTP id g27so149935dan.38 for ; Sun, 17 Mar 2013 17:40:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=6wjS3SuluNqmVatZ2QU4kd5q6rzYyVs288HyoJCP5tw=; b=RdJMawnqFdh3T+9IYGskShFrmm6v+I5KDBmZvmcv8MjRvaSd0YTWS0mlmOPS3korN4 9qATkzU5UOnuk0ozG3RpCHmx3C9vIY9+nIq8fd3tM65gVU4dOpyP7OH5QwnAaME5/9Iz +dSnjLlqBt9J8xa2l44B2hnP+tuVjmPMkE4ihe1AtkwG3KdMe708b3n66uP7nX5MmtV4 m7PC4TYOXQKvz6enTcBMVNkkxDXeLkssbEfLTJZQG9mKvMDc/CvEBK46T56aCd4rwfbV wL81JWq+gCnvoHiwkYB6YEh2W+1knCqtEsmf1PQdjaGfps0pa9BKdrvkT+uJy5LuZXXa YgOw== MIME-Version: 1.0 X-Received: by 10.66.251.7 with SMTP id zg7mr7469755pac.123.1363567220872; Sun, 17 Mar 2013 17:40:20 -0700 (PDT) Sender: thbley@gmail.com Received: by 10.68.93.130 with HTTP; Sun, 17 Mar 2013 17:40:20 -0700 (PDT) In-Reply-To: References: Date: Mon, 18 Mar 2013 01:40:20 +0100 X-Google-Sender-Auth: gRTa7cfwcxvbfBIrQ-89pfxvCrM Message-ID: To: PHP Development Cc: Daniele Orlando , pierre@pcservice.co.za Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] __invokeStatic() method From: thbley+php@gmail.com (Thomas Bley) 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