php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30716 Dynamic Static Member Access
Submitted: 2004-11-08 05:59 UTC Modified: 2010-12-22 03:23 UTC
Votes:17
Avg. Score:4.8 ± 0.5
Reproduced:16 of 16 (100.0%)
Same Version:6 (37.5%)
Same OS:8 (50.0%)
From: jake at development dot mindvision dot com dot au Assigned: johannes (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.1 OS: Gentoo Linux (2.4.x)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jake at development dot mindvision dot com dot au
New email:
PHP Version: OS:

 

 [2004-11-08 05:59 UTC] 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 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-09 09:27 UTC] 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-11 09:22 UTC] 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-11 09:54 UTC] tony2001@php.net
use call_user_func() for that.
 [2005-12-21 00:23 UTC] 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
 [2007-10-05 01:45 UTC] itisben at gmail dot com
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
 [2010-12-22 03:23 UTC] johannes@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: johannes
 [2010-12-22 03:23 UTC] johannes@php.net
"Dynamic static calls" were added in PHP 5.3.0
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 29 14:01:32 2024 UTC