Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:28276 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34377 invoked by uid 1010); 6 Mar 2007 15:06:38 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 34361 invoked from network); 6 Mar 2007 15:06:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Mar 2007 15:06:38 -0000 Authentication-Results: pb1.pair.com header.from=colder@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=colder@php.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain php.net from 213.239.212.54 cause and error) X-PHP-List-Original-Sender: colder@php.net X-Host-Fingerprint: 213.239.212.54 dns1.omne-serveurs.net Linux 2.4/2.6 Received: from [213.239.212.54] ([213.239.212.54:50567] helo=dns1.omne-serveurs.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/FD-08626-6738DE54 for ; Tue, 06 Mar 2007 10:06:31 -0500 Received: (qmail 31961 invoked from network); 6 Mar 2007 15:06:26 -0000 Received: from 81.62.5.171 by dns1 (envelope-from , uid 1004) with qmail-scanner-1.23 (clamdscan: 0.82. spamassassin: 3.0.1. perlscan: 1.32. Clear:RC:1(81.62.5.171):. Processed in 0.100725 secs); 06 Mar 2007 15:06:26 -0000 Received: from 171.5.62.81.cust.bluewin.ch (HELO ?10.0.0.3?) (81.62.5.171) by dns1.omne-serveurs.net with (DHE-RSA-AES256-SHA encrypted) SMTP; 6 Mar 2007 15:06:26 -0000 Message-ID: <45ED838F.9040508@php.net> Date: Tue, 06 Mar 2007 16:06:55 +0100 User-Agent: Thunderbird 1.5.0.7 (X11/20060909) MIME-Version: 1.0 To: internals@lists.php.net Content-Type: multipart/mixed; boundary="------------040105040102020303060500" Subject: [patch] Dynamic access of static members/methods, and constants From: colder@php.net (Etienne Kneuss) --------------040105040102020303060500 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, It may have already been proposed, but I give it a shot: I believe it would be nice to be able to dynamically reference static members and constants. 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 = 'bar'; const gee = 'zzz'; public static function getFoo() { return self::$foo; } } $classname = 'A'; echo $classname::$foo; // bar echo $classname::gee; // zzz // instead of: echo call_user_func(array($classname, 'getFoo')); Please take a look at the patch I made for it. For consistency purposes, I also implemented $classname::myMethod(); even if it's not required. FWIW, I've no strong feeling about it. Regards, -- 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 --------------040105040102020303060500 Content-Type: text/plain; name="dynamic-static-calls.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="dynamic-static-calls.patch" 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 15:05:19 -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 15:05:19 -0000 @@ -0,0 +1,14 @@ +--TEST-- +Dynamic access of static members +--FILE-- + +===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 15:05:19 -0000 @@ -0,0 +1,14 @@ +--TEST-- +Dynamic access of constants +--FILE-- + +===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 15:05:19 -0000 @@ -0,0 +1,14 @@ +--TEST-- +Dynamic call for static methods +--FILE-- + +===DONE=== +--EXPECT-- +bar +===DONE=== --------------040105040102020303060500--