php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55839 Static accessors through variables only work on simple variables
Submitted: 2011-10-03 18:47 UTC Modified: 2011-10-03 20:37 UTC
From: chris at mcfadyen dot ca Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3SVN-2011-10-03 (SVN) OS: Windows
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: chris at mcfadyen dot ca
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-03 18:53 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-10-03 18:53 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Variable names are case-sensitive. $B is not the same as $b.

${$a->referrer}::foo(); in your case is the same as $B::foo()m not $b::foo();
 [2011-10-03 20:17 UTC] chris at mcfadyen dot ca
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"
 [2011-10-03 20:37 UTC] johannes@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 01 01:01:28 2024 UTC