Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66665 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54389 invoked from network); 16 Mar 2013 20:08:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2013 20:08:06 -0000 Authentication-Results: pb1.pair.com header.from=simon@simon.geek.nz; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=simon@simon.geek.nz; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain simon.geek.nz designates 103.6.213.4 as permitted sender) X-PHP-List-Original-Sender: simon@simon.geek.nz X-Host-Fingerprint: 103.6.213.4 simon.geek.nz Received: from [103.6.213.4] ([103.6.213.4:42280] helo=simon.geek.nz) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 77/B6-08095-521D4415 for ; Sat, 16 Mar 2013 15:08:06 -0500 Received: from [10.0.1.2] (121-73-68-50.cable.telstraclear.net [121.73.68.50]) by simon.geek.nz (Postfix) with ESMTPSA id A37D0605C0; Sun, 17 Mar 2013 09:08:01 +1300 (NZDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\)) In-Reply-To: Date: Sun, 17 Mar 2013 09:07:59 +1300 Cc: internals@lists.php.net Content-Transfer-Encoding: 7bit Message-ID: <5C6AFB90-339A-42BB-9F55-E85516D10BE6@simon.geek.nz> References: To: Daniele Orlando X-Mailer: Apple Mail (2.1503) Subject: Re: [PHP-DEV] __invokeStatic() method From: simon@simon.geek.nz (Simon J Welsh) On 17/03/2013, at 8:33 AM, 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 Function and class names can overlap. So: function a() { return "A"; } class A {} is valid. --- Simon Welsh Admin of http://simon.geek.nz/