|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-10-03 18:47 UTC] chris at mcfadyen dot ca
Description:
------------
Using a static operator ( :: ) works for simple variables ( $foo::function() ),
but results in an undefined variable warning for the class name at run-time when
using a class property ( ${$bar->foo}::function() ). Obviously, a direct access
attempt ( $bar->foo::function() ) fails with a parse error.
---
From manual page: http://www.php.net/language.oop5.static
---
Test script:
---------------
class A {
public $referrer = 'B';
}
class B {
public static function foo() {
echo "Foo";
}
}
$b = 'B';
$a = new A();
$b::foo();
${$a->referrer}::foo();
$c = $a->referrer;
$c::foo();
Expected result:
----------------
I would expect to see
Foo
Foo
Foo
Actual result:
--------------
This is the result:
Foo
Notice (8): Undefined variable: B
Foo
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sat Feb 14 04:00:01 2026 UTC |
It's not supposed to reference $b, it's supposed to reference the class B $b::foo() is de-referenced as B::foo() Why would ${$a->referrer}::foo() not de-reference to B::foo() as well? To make it more obvious so that you can't misinterpret it: class ClassOne { public $referrer = 'ClassTwo'; } class ClassTwo { public static function foo() { echo "Foo"; } } $a = 'ClassTwo'; $b = new ClassOne(); $a::foo(); ${$b->referrer}::foo(); $c = $b->referrer; $c::foo(); Which gives the notice "Undefined variable: ClassTwo"Here is the reduced case: class ClassOne { public $referrer = 'ClassTwo'; } $b = new ClassOne(); echo ${$b->referrer}; ${$b->referrer}; is $classTwo. That variable does not exist.