Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:29156 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7422 invoked by uid 1010); 3 May 2007 14:50:15 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 7407 invoked from network); 3 May 2007 14:50:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2007 14:50:15 -0000 Authentication-Results: pb1.pair.com header.from=diogin@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=diogin@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 64.233.166.183 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: diogin@gmail.com X-Host-Fingerprint: 64.233.166.183 py-out-1112.google.com Linux 2.4/2.6 Received: from [64.233.166.183] ([64.233.166.183:1420] helo=py-out-1112.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B9/52-27633-5A6F9364 for ; Thu, 03 May 2007 10:50:14 -0400 Received: by py-out-1112.google.com with SMTP id a25so445612pyi for ; Thu, 03 May 2007 07:50:11 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=cr9T1SHHKOsy4vWd3vEPKtiROkFfKvdoFJNsOHLiboEokC2Sh1wNv1kWZBDj+I2BuvrwyRaybdciTQbTazls+3kIB0aWGvqN+arc10Lno/djcfzcJSFxgYdy9Tvbrb44Dw6WikOtZ2FmKdvxqyi/cVwFiOkxuKiwUEZKDoYGL4A= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=OltOzrTK6u8F4PaWEkBoTfVxe5lBUaQehBguvo48UNNxyMeUW2oYlJdGiy0+uypnoUZdUQnm+fROtwkTaolYup1MMiwctasVOgvp2c6cCDa46gnx3FIe6VH09Cf/Zyb44k6KzG0w38p4aaI5yikJ0QuFU/G1NKcQfpbC0W8bX+s= Received: by 10.35.62.19 with SMTP id p19mr3426411pyk.1178203810674; Thu, 03 May 2007 07:50:10 -0700 (PDT) Received: by 10.35.121.17 with HTTP; Thu, 3 May 2007 07:50:10 -0700 (PDT) Message-ID: Date: Thu, 3 May 2007 22:50:10 +0800 To: "Etienne Kneuss" , internals In-Reply-To: <4631EE73.4050409@php.net> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_13391_15099402.1178203810595" References: <4631EE73.4050409@php.net> Subject: Re: [PHP-DEV] [patch] Dynamic access of static members/methods, and constants From: diogin@gmail.com ("Jingcheng Zhang") ------=_Part_13391_15099402.1178203810595 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, In this way, classes can be considered as polymorphism, just like objects. So is it OK to allow abstract static function in abstract class to support this feature directory? Currently it is forbidden in HEAD, only interfaces can have static function. Thanks 2007/4/27, Etienne Kneuss : > > Hi, > > I've already proposed that 1-2 months ago, but now seems to be a good > time to discuss new things, so let's try again: > > I believe it would be nice to be able to dynamically reference static > members, constants, and methods. > > Currently, there is no way to do that and the only way would be to > create a static method and use call_user_func(array($classname, > 'getThat')); > > In other words: > > class A { > public static $foo = 'value1'; > const gee = 'value2'; > public static function bar() { return 'value3'; } > > // old way: > public static function getFoo() { return self::$foo; } > public static function getGee() { return self::gee; } > > } > > $classname = 'A'; > echo $classname::$foo; // value1 > echo $classname::gee; // value2 > echo $classname::bar(); // value3 > > // --- instead of: --- > > $classname = 'A'; > echo call_user_func(array($classname, 'getFoo')); // value1 > echo call_user_func(array($classname, 'getGee')); // value2 > echo call_user_func(array($classname, 'bar')); // value3 > > which is quite slow. > > Please take a look at the patch[1] I made for it. Comments would be > appreciated! > > Regards, > > > 1: http://patches.colder.ch/Zend/dynamic-static-calls.patch?markup > > -- > Etienne Kneuss > http://www.colder.ch > colder@php.net > > Men never do evil so completely and cheerfully as > when they do it from a religious conviction. > -- Pascal > > Index: Zend/zend_language_parser.y > =================================================================== > RCS file: /repository/ZendEngine2/zend_language_parser.y,v > retrieving revision 1.179 > diff -u -r1.179 zend_language_parser.y > --- Zend/zend_language_parser.y 4 Mar 2007 16:25:57 -0000 1.179 > +++ Zend/zend_language_parser.y 6 Mar 2007 14:43:49 -0000 > @@ -642,6 +642,12 @@ > | fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM > variable_without_objects '(' { zend_do_end_variable_parse(BP_VAR_R, 0 > TSRMLS_CC); zend_do_begin_class_member_function_call(&$1, &$3 TSRMLS_CC); } > function_call_parameter_list > ')' { zend_do_end_function_call(NULL, &$$, &$6, 1, > 1 TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C);} > + | variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' { > zend_do_begin_class_member_function_call(&$1, &$3 TSRMLS_CC); } > + function_call_parameter_list > + ')' { zend_do_end_function_call(NULL, &$$, &$6, 1, > 1 TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C);} > + | variable_class_name T_PAAMAYIM_NEKUDOTAYIM > variable_without_objects '(' { zend_do_end_variable_parse(BP_VAR_R, 0 > TSRMLS_CC); zend_do_begin_class_member_function_call(&$1, &$3 TSRMLS_CC); } > + function_call_parameter_list > + ')' { zend_do_end_function_call(NULL, &$$, &$6, 1, > 1 TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C);} > | variable_without_objects '(' { > zend_do_end_variable_parse(BP_VAR_R, 0 TSRMLS_CC); > zend_do_begin_dynamic_function_call(&$1 TSRMLS_CC); } > function_call_parameter_list ')' > { zend_do_end_function_call(&$1, &$$, &$4, 0, 1 > TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C);} > @@ -795,6 +801,11 @@ > > static_member: > fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM > variable_without_objects { $$ = $3; zend_do_fetch_static_member(&$$, &$1 > TSRMLS_CC); } > + | variable_class_name T_PAAMAYIM_NEKUDOTAYIM > variable_without_objects { $$ = $3; zend_do_fetch_static_member(&$$, &$1 > TSRMLS_CC); } > +; > + > +variable_class_name: > + reference_variable { zend_do_end_variable_parse(BP_VAR_R, 0 > TSRMLS_CC); zend_do_fetch_class(&$$, &$1 TSRMLS_CC); } > ; > > > @@ -930,6 +941,7 @@ > > class_constant: > fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING > { zend_do_fetch_constant(&$$, &$1, &$3, ZEND_RT TSRMLS_CC); } > + | variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { > zend_do_fetch_constant(&$$, &$1, &$3, ZEND_RT TSRMLS_CC); } > ; > > %% > Index: tests/lang/041.phpt > =================================================================== > RCS file: tests/lang/041.phpt > diff -N tests/lang/041.phpt > --- /dev/null 1 Jan 1970 00:00:00 -0000 > +++ tests/lang/041.phpt 6 Mar 2007 14:43:49 -0000 > @@ -0,0 +1,14 @@ > +--TEST-- > +Dynamic access of static members > +--FILE-- > + +class A { > + public static $b = 'foo'; > +} > +$classname = 'A'; > +echo $classname::$b; > +?> > +===DONE=== > +--EXPECT-- > +foo > +===DONE=== > Index: tests/lang/042.phpt > =================================================================== > RCS file: tests/lang/042.phpt > diff -N tests/lang/042.phpt > --- /dev/null 1 Jan 1970 00:00:00 -0000 > +++ tests/lang/042.phpt 6 Mar 2007 14:43:49 -0000 > @@ -0,0 +1,14 @@ > +--TEST-- > +Dynamic access of constants > +--FILE-- > + +class A { > + const B = 'foo'; > +} > +$classname = 'A'; > +echo $classname::B; > +?> > +===DONE=== > +--EXPECT-- > +foo > +===DONE=== > Index: tests/lang/043.phpt > =================================================================== > RCS file: tests/lang/043.phpt > diff -N tests/lang/043.phpt > --- /dev/null 1 Jan 1970 00:00:00 -0000 > +++ tests/lang/043.phpt 6 Mar 2007 14:43:49 -0000 > @@ -0,0 +1,14 @@ > +--TEST-- > +Dynamic call for static methods > +--FILE-- > + +class A { > + static function foo() { return 'bar'; } > +} > +$classname = 'A'; > +echo $classname::foo(); > +?> > +===DONE=== > +--EXPECT-- > +bar > +===DONE=== > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > -- Best regards, Jingcheng Zhang Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University P.R.China ------=_Part_13391_15099402.1178203810595--