php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47620 Weird static keyword behaviour
Submitted: 2009-03-11 07:18 UTC Modified: 2009-03-18 15:02 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: olemarkus dot with at student-media dot no Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3CVS-2009-03-11 (snap) OS: *
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: olemarkus dot with at student-media dot no
New email:
PHP Version: OS:

 

 [2009-03-11 07:18 UTC] olemarkus dot with at student-media dot no
Description:
------------
When using static:: in a parent class to refer to the child class that was called, the keyword is resolved to the caller class instead.


Reproduce code:
---------------
http://olemarkus.com/issue.txt

Expected result:
----------------
I expect both calls to \C\C::getName() to yield the same result:
C
C

Actual result:
--------------
The second one gives the following output:
C

Fatal error: Access to undeclared static property: A\A::$name in issue.php on line 13


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-11 14:05 UTC] jani@php.net
And here is the code: 

<?php
namespace A {
  class A  {
    function __construct() {
      echo \C\C::getName() . "\n";
    }	
  }
}
namespace B {
  abstract class B {
    static protected $name = '';
    function getName() {
      return static::$name;
    }
  } 
}
namespace C {
  class C extends \B\B {
    static protected $name  = 'C';
  }
}
namespace {  
  echo \C\C::getName() . "\n";
  $a = new \A\A();
}
?>

 [2009-03-18 15:02 UTC] dmitry@php.net
It's hard to explain, but it's expected.

In case the method getName() doesn't declared as "static" it's called dynamically with current $this (A). This feature is kept for ages for backward compatibility. You can even see "strict" warning:

Strict Standards: Non-static method B::getName() should not be called statically, assuming $this from incompatible context in ...

In case you declare getName() "static" you'll get that you expect.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Sep 27 23:01:26 2024 UTC