我遇到一个奇怪的问题。
下面粘贴的代码在HHVM(版本3.1.0)中工作,但在PHP(版本5.5.9)中失败,并出现非常奇怪的错误消息。
<?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;
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();
}
当我在HHVM中运行此代码时,它正确地响应'yyy'。
但在PHP中,我得到以下错误信息:"PHP致命错误:访问未声明的静态属性:DummyTest'Lib'Database'TPDO::$config in/home/……/bugtest/staticclass. PHP on line 50"
我看不出为什么会失败。类'DummyTest'SystemConfig不从任何类继承,类'DummyTest'Lib'Database'TPDO不从'DummyTest'SystemConfig继承。那么PHP为什么会失败呢?
如果我在'DummyTest'SystemConfig中更改static::$config[$name]到self::$config[$name],它在PHP和HHVM中都能按预期工作。
这段代码是从一个较大的项目中提取出来的。但是提取的代码的行为与较大的项目相同。
关于当前环境的更多信息是,它是一台运行Ubuntu 14.04的计算机,代码是从命令行执行的。
Get()应该是静态的
public static function Get($name)