#30716 [Com]: Dynamic Static Member Access
| From: | itisben at gmail dot com | Date: | Fri, 05 Oct 2007 01:45:28 +0000 |
| Subject: | #30716 [Com]: Dynamic Static Member Access | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-118195@lists.php.net to get a copy of this message | ||
ID: 30716
Comment by: itisben at gmail dot com
Reported By: jake at development dot mindvision dot com dot au
Status: Open
Bug Type: Feature/Change Request
Operating System: Gentoo Linux (2.4.x)
PHP Version: 5.0.1
New Comment:
Hi,
In case anyone is still beating their head against the wall -or-
doesn't want to use eval -or- isn't sure whether or not 5.2.4 fixed this
in addition to the dynamic static method call that was supposed to be
fixed in that version - http://bugs.php.net/bug.php?id=31318 (I can't
get it to work after moving to that version, unless the little note at
the bottom means that it is 5.3).
Anway, here it is - it requires PHP >=5 for the Reflection
<?php
...
$refClass = new ReflectionClass($className);
$staticMemVal = $refClass->getStaticPropertyValue($staticMemName);
...
?>
Hopefully the variable names are descriptive enough to get the general
idea.
Thanks,
Ben
Previous Comments:
------------------------------------------------------------------------
[2005-12-21 00:23:58] jake at press dot id dot au
Heyas
I havent got a copy of php infront of me, and it has been many months
since i last poked around on this issue - so apologies if i am wrong...
But i dont beleive "call_user_func()" is the solution.
... we are trying too access a static member dynamically, not a static
method.
Such a pity, i have been successfully using a workaround along the same
lines as suggested by joscha at feth dot com for along time now, i was
hoping i could go thru and fix all of my code that is dependant on it by
now :(
I'll try and find some time too investigate this further and attempt to
fix - but i fear that will be closer to this time next year.
Jake Press
------------------------------------------------------------------------
[2004-11-11 09:54:49] tony2001@php.net
use call_user_func() for that.
------------------------------------------------------------------------
[2004-11-11 09:22:32] joscha at feth dot com
After I found out, that my note (http://news.php.net/php.notes/78312)
was deleted, which I thought was not a good idea, because it is the only
way to make a static function call to a "unknown" class, I wrote a Mail
to the guy who deleted it. After a few mails we decided that the code I
wrote in the note:
print_r(eval("return $classname::getBla();"));
is completely correct and working. So I resubmitted my note, but I also
think it'd be better to allow $classname::getBla(); out-of-the-box as
the eval call is always a performance killer and it restricts the use of
the code above to examples where no user-input is in $classname.
------------------------------------------------------------------------
[2004-11-09 09:27:15] k at ailis dot de
Just for info: The problem is also present in latest CVS. And even if
it is not a bug it's still a useful feature request.
------------------------------------------------------------------------
[2004-11-08 05:59:14] jake at development dot mindvision dot com dot au
Description:
------------
Hello,
I feel i have a syntax bug or unclear syntax limitation.
I am trying to access static members of classes, dynamically
ie $foo = 'className'; echo $foo::$staticVariable;
For more information please see my posts on the php-generals list
- http://news.php.net/php.general/201458
Thanks in advance
Yours Sincerely
Jake Press
Reproduce code:
---------------
<?php
// Define a class with a nice juicy static variable
class bob {
public static $type = 'safd';
}
// Lets chuck the name of the class into a variable...
$foo = 'bob';
// Just to show it all works lets do it manualllllly
echo bob::$type;
// This is the line with the well-wicked parse error on the
double colon
echo $foo::$type;
?>
Expected result:
----------------
safdsafd
Actual result:
--------------
Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting
',' or ';' in /mnt/data0/www/twd/test.php on line 15
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30716&edit=1