-1

I've encountered a strange problem.
The code pasted below works in HHVM (version 3.1.0) but it fails in PHP (version 5.5.9) with a very strange error message.

<?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();
}

When I run this code in HHVM it correctly responds 'yyy'.
But in PHP I get the following error message: "PHP Fatal error: Access to undeclared static property: DummyTest\Lib\Database\TPDO::$config in /home/.../bugtest/staticclass.php on line 50"

I can't see why it fails. The class \DummyTest\SystemConfig doesn't inherit from any class and the class \DummyTest\Lib\Database\TPDO doesn't inherit from \DummyTest\SystemConfig. So why does PHP fail?
If I change static::$config[$name] in \DummyTest\SystemConfig to self::$config[$name] it works as expected in both PHP and HHVM.

This code is extracted from a larger project. But the extracted code behaves identically to the larger project.

More information about the current environment is that it is a computer running Ubuntu 14.04 and the code is executed from the command line.

  • I wonder why don't you get a strict standards error for calling non-static method staticly – Royal Bg Jul 15 '14 at 14:38
  • Me too. I've been sitting with this for the entire day. A proper error message would have made it much easier. This however turns from a PHP related problem to a HHVM related problem since HHVM gives an incorrect answer. – user2174187 Jul 15 '14 at 15:03

1 Answers1

1

Get() should be static

public static function Get($name)
meda
  • 45,103
  • 14
  • 92
  • 122