Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66667 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58617 invoked from network); 16 Mar 2013 20:33:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2013 20:33:11 -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.56.216.30 as permitted sender) X-PHP-List-Original-Sender: pierre@pcservice.co.za X-Host-Fingerprint: 69.56.216.30 gateway07.websitewelcome.com Linux 2.6 Received: from [69.56.216.30] ([69.56.216.30:34730] helo=gateway07.websitewelcome.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 99/77-08095-507D4415 for ; Sat, 16 Mar 2013 15:33:10 -0500 Received: by gateway07.websitewelcome.com (Postfix, from userid 5007) id 419DBB622B766; Sat, 16 Mar 2013 15:33:02 -0500 (CDT) Received: from vinacomin.websitewelcome.com (vinacomin.websitewelcome.com [50.97.101.199]) by gateway07.websitewelcome.com (Postfix) with ESMTP id 357B6B622B732 for ; Sat, 16 Mar 2013 15:33:02 -0500 (CDT) Received: from [209.85.216.49] (port=43789 helo=mail-qa0-f49.google.com) by vinacomin.websitewelcome.com with esmtpsa (TLSv1:RC4-SHA:128) (Exim 4.80) (envelope-from ) id 1UGxms-0005kQ-CQ for internals@lists.php.net; Sat, 16 Mar 2013 15:33:06 -0500 Received: by mail-qa0-f49.google.com with SMTP id o13so950841qaj.15 for ; Sat, 16 Mar 2013 13:33:05 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.229.76.89 with SMTP id b25mr2857245qck.31.1363465985869; Sat, 16 Mar 2013 13:33:05 -0700 (PDT) Reply-To: pierre@pcservice.co.za Received: by 10.49.129.134 with HTTP; Sat, 16 Mar 2013 13:33:05 -0700 (PDT) Received: by 10.49.129.134 with HTTP; Sat, 16 Mar 2013 13:33:05 -0700 (PDT) In-Reply-To: References: Date: Sat, 16 Mar 2013 22:33:05 +0200 Message-ID: To: Daniele Orlando Cc: PHP Development Content-Type: multipart/alternative; boundary=00235429dfc4c05c0e04d810a964 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-qa0-f49.google.com) [209.85.216.49]:43789 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) --00235429dfc4c05c0e04d810a964 Content-Type: text/plain; charset=ISO-8859-1 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. --00235429dfc4c05c0e04d810a964--