#45728 [Csd]: Static references via Variable Variables
| From: | thinice at gmail dot com | Date: | Thu, 07 Aug 2008 15:26:54 +0000 |
| Subject: | #45728 [Csd]: Static references via Variable Variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-127736@lists.php.net to get a copy of this message | ||
ID: 45728
User updated by: thinice at gmail dot com
Reported By: thinice at gmail dot com
Status: Closed
Bug Type: Scripting Engine problem
Operating System: FreeBSD
PHP Version: 5.2.6
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2008-08-07 15:16:11] 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:07:56] 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:01:59] 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 09:53:09] jani@php.net
Where exactly it says this should work..?
------------------------------------------------------------------------
[2008-08-06 02:00:46] 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:
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45728&edit=1