php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67625 Access to undeclared static property
Submitted: 2014-07-15 14:56 UTC Modified: 2014-07-16 14:20 UTC
From: tb at bytecode dot se Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.5.14 OS: Ubuntu 14.04
Private report: No CVE-ID: None
 [2014-07-15 14:56 UTC] tb at bytecode dot se
Description:
------------
I'm running a plain vanilla PHP installation in Ubuntu (both the standard 5.5.9 and the developer version available which is 5.5.14).
When I run the script below from the command line I get the error message "PHP Fatal error:  Access to undeclared static property: DummyTest\Lib\Database\TPDO::$config in /home/thomas/src/bugtest/staticclass.php on line 50"

If I change all static::$config[$name] to self::$config[$name] then it works without errors.

I've also tested the same code in HHVM version 3.1.0 without any errors. (Tested both with static:: and self:: )

On line 49 I call get_called_class() to check which class is called. In my tiny little world I would expect it to say "DummyTest\SystemConfig" which it does in HHVM. But in PHP it says "DummyTest\Lib\Database\TPDO".



Test script:
---------------
<?php
namespace DummyTest\Lib {
  class TObject {
    public function __construct() {
    }
  }
  class TComponent extends \DummyTest\Lib\TObject {
    public function __construct() {
      parent::__construct();
    }
  }
  class TSingletonComponent extends \DummyTest\Lib\TComponent {
    public function __construct() {
      parent::__construct();
    }
  }
  class TDatabase extends \DummyTest\Lib\TSingletonComponent {
    public function __construct() {
      parent::__construct();
    }

    public static function Init() {
      $db = new \DummyTest\Lib\Database\TPDO();
    }
  }
}

namespace DummyTest\Lib\Database {
  class TDatabase extends \DummyTest\Lib\TComponent {
    public function __construct() {
      parent::__construct();
    }
  }
  class TPDO extends \DummyTest\Lib\Database\TDatabase {
    protected $pdo;

    public function __construct() {
      parent::__construct();
      echo \DummyTest\SystemConfig::Get('xxx'); // <-- This fails in PHP
    }
  }
}

namespace DummyTest {
  class SystemConfig {
    protected static $config = array();

    public function Get($name) {
      echo get_called_class().PHP_EOL; // <-- Gives different result in PHP and HHVM
      if(isset(static::$config[$name])) {
        return static::$config[$name];
      }
      return null;
    }

    public function Set($name, $value) {
      static::$config[$name] = $value;
    }
  }

  SystemConfig::Set('xxx', 'yyy');
  \DummyTest\Lib\TDatabase::Init();
}

Expected result:
----------------
DummyTest\SystemConfig
yyy

Actual result:
--------------
DummyTest\Lib\Database\TPDO
PHP Fatal error:  Access to undeclared static property: DummyTest\Lib\Database\TPDO::$config in /home/thomas/src/bugtest/staticclass.php on line 50

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-15 15:01 UTC] tb at bytecode dot se
-Status: Open +Status: Closed
 [2014-07-15 15:01 UTC] tb at bytecode dot se
Stupid misstake where I forgot to declare the Get/Set methods as static.
So the problem will be moved over to the guys at Facebook instead =)
 [2014-07-16 14:20 UTC] rasmus@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Feb 28 16:01:33 2024 UTC