php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45728 Static references via Variable Variables
Submitted: 2008-08-06 02:00 UTC Modified: 2008-08-07 15:26 UTC
From: thinice at gmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.6 OS: FreeBSD
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: thinice at gmail dot com
New email:
PHP Version: OS:

 

 [2008-08-06 02:00 UTC] thinice at gmail dot com
Description:
------------
When you try to access a static variable within a class using a variable variable to determine which class and static to use.

Reproduce code:
---------------
<?
class MyTest {
	static $myVar = 'testval';
}

$sClass = 'MyTest';

echo "Expected output: ".MyTest::$myVar;
echo "<br/>";
$s = $sClass.'::$myVar';
echo "Using this as the string: ".$s."<br/>";
echo "Using Variable-named reference: ".${$s}; 
?>

Expected result:
----------------
Expected output: testval
Using this as the string: MyTest::$myVar
Using Variable-named reference: testval

Actual result:
--------------
Expected output: testval
Using this as the string: MyTest::$myVar
Using Variable-named reference:

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-07 09:53 UTC] jani@php.net
Where exactly it says this should work..?
 [2008-08-07 15:01 UTC] thinice at gmail dot com
You can access non-statics this way.

$myClass = 'MyClass';
echo $myClass->anythingElse;

Why wouldn't you be able to access variables via :: ?

I don't see anything on whether this should, or should not work. But why shouldn't/wouldn't it be possible to access a static variable using a variable to set the object name?
 [2008-08-07 15:07 UTC] thinice at gmail dot com
All I can see in documentation is that you can't use :: for accessing the static from an instantiated object.

e.g.: Documentation explicitly says this will -not- work:
$myClass = new MyClass;
echo $myClass::myVar;

But this isn't the problem.
 [2008-08-07 15:16 UTC] colder@php.net
As of PHP5.3 you can do

class A {
   public static $foo = 2;
}
$a = new A;

echo $a::$foo; // 2

$a used in a classname context will simply be converted to the class of the object.

so it's equivalent to:
$n = get_class($a);
echo $n::$foo; // 2
 [2008-08-07 15:26 UTC] thinice at gmail dot com
You beat me to it, I just noticed the http://us.php.net/manual/en/language.oop5.paamayim-nekudotayim.php '::' page was updated for 5.3.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 21:01:32 2025 UTC