Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:45226 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63619 invoked from network); 6 Aug 2009 20:18:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Aug 2009 20:18:33 -0000 Authentication-Results: pb1.pair.com smtp.mail=kenashkov@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=kenashkov@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.224 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: kenashkov@gmail.com X-Host-Fingerprint: 209.85.219.224 mail-ew0-f224.google.com Received: from [209.85.219.224] ([209.85.219.224:42921] helo=mail-ew0-f224.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/75-37691-79A3B7A4 for ; Thu, 06 Aug 2009 16:18:32 -0400 Received: by ewy24 with SMTP id 24so1265260ewy.23 for ; Thu, 06 Aug 2009 13:18:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=0/ijIcReftdyPYWlwFcuezYovoHc4buVzOWFoyVdhNY=; b=qdfDNe1Y6ZN1NvuZrfINO263XWQlSLGMrtrhCZ76rXUfbWBRtesmShV8xCkbDdpmFw tJ/1rSTkg4lRHRpTfHnlGuDJFJznYbs5luf3QETUa3GFJUgvAcHTCUYmKk0JYvY5wEik 453xUkyp/h3qO7gbuJ0AbJxlSX0CU3xhc9eC4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=Jtxs0hjpwKs7tZwd8aKJIPSYfonOEfvKslJqJKZHJj0UgXyncBFBbEF30AJDmrnn2I hd181Phk5WPaYQRaVQUxi/pcnFmy9uDOSzYSoki366+cquWJz5WicytDoXUCS+QO+9CD +1hdKQRPZCGvz/Pkroa2ZlwUFUELo0oKA1fgc= MIME-Version: 1.0 Received: by 10.216.11.212 with SMTP id 62mr52398wex.186.1249589895743; Thu, 06 Aug 2009 13:18:15 -0700 (PDT) Date: Thu, 6 Aug 2009 23:18:15 +0300 Message-ID: <261daaa10908061318t18e7337o7343e4d9ab443bd2@mail.gmail.com> To: PHP Internals List Content-Type: multipart/alternative; boundary=0016364d23ddd9c91f04707ed101 Subject: RFC: __invokeStatic() method From: kenashkov@gmail.com (Vesselin Kenashkov) --0016364d23ddd9c91f04707ed101 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hello everyone, I tried to create a page in the wiki but I dont see the "create" button. I have an account but I guess I still lack permissions. Anyway here is my proposal: Implementation of magic method __invokeStatic Here is the specific case that gave me the idea for the need of __invokeStatic. file translate.php: internalTranslate($string); } public function __invoke($string) { $this->internalTranslate(); } public static function __invokeStatic() { // the new magic method return self::$instance->internalTranslate($string); } private function internalTranslate($string) { return $string; } } ?> In the following file invoke.php are shown the possible ways of invokation of the function & the methods of the class: ('something'); // [2] t::t('something'); // [3] t::$i('something') // [4] (unsupported at the moment) t('something'); // [5] (this is what the proposal is about) ?> In the real word functions like translate() are invoked very often and is good to havea very short way to call them. Here are the options: [1] - as the functions can not be imported with different name we have to import the ns as something very short. But still we have to type f\translate. We can shortne it even more if we rename the function from translate() to t() and then it will be f\t(). But we still can not avoid refenrecing the ns. [2] - the calsses can be imported with different name and this saves a lot. Then we can get/create an instance and invoke the translation using the existing __invoke(). It is shortest than any invokation of methods like $t->_(); for example. But this is too long [3] - a short way to call it. But can it be even shorter? [4] - it is not actually shorter - it implies that the $instance is public static $i. The idea is to get the public instance and use the dynamic __invoke. Unfortuately at the moment the php syntax does not allow this. (anyway it is not shorter than t::t()). [5] - a call similar to the $t() but in static context. Here is supposed the __callStatic to be invoked. The shortest... The problems: The biggest problem is that there is the collision with a function defined in some\other\path namespace called t. But fortunately this is (as far as I can think) the only possible collision and I think it will never happen in a modern namespaced, properly organized code, as the functions will be in other files and namespaces, while the invoke.php file will contain only calls but not definitions. And of course it may confuse the developers (a function or a static invokation of a method?). But since a lot of time we have $something() and now we have $object(). As well in 5.3 was added some magic for static (__callStatic). Positives: We can simulate functions this way! (actually I'm not pretty sure is this good or bad). Simulating functions this way we can import them in the namespaces (just like classes because they are in fact classes). This way any function can be transparently converted to a class (with whatever name) by defining __invokeStatic method and importing with an alias the class in the working namespace (or without alias by just nameing the class with the name of the function). But I think the main question is - if the naming conflict can be resolved - do you think this would be useful? I think it will be because this way all the php code can be converted to OOP (I'm not a zealot of OOP, just discussing the possibilities here). And if it is useful is it possible to be implemented (my C knowledge is near 0 and my php internals is absolute 0)? If the topic is found interesting and I get somehow permission to create a wiki page I will happily do a RFC there. Vesselin Kenashkov P.S. a little offtopic but looking back at [4] - can it be done to support the t::$i('something'); call? or {t::$i}('something'); ? --0016364d23ddd9c91f04707ed101--