Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A function not declared static may be treated as static #3193

Closed
delith opened this issue Jul 15, 2014 · 5 comments
Closed

A function not declared static may be treated as static #3193

delith opened this issue Jul 15, 2014 · 5 comments

Comments

@delith
Copy link

delith commented Jul 15, 2014

I was sitting with a problem the entire day and it turned out that I had missed to declare a function as static. When I ran PHP (5.5.9 and 5.5.14) it failed but in HHVM (5.1.0) it gave the same result as I expected if I had written public static function instead of public function.

The error message from PHP was: "PHP Fatal error: Access to undeclared static property: DummyTest\Lib\Database\TPDO::$config in /home/thomas/src/bugtest/staticclass.php on line 50".

It later turned out that the functions
public function Get($name)
public function Set($name, $value)
Should have been declared
public static function Get($name)
public static function Set($name, $value)

However. When I ran the code with errors if passed correctly through HHVM. It even printed "DummyTest\SystemConfig" as a response to get_called_class(). The call went on as if it were declared as static.

<?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 serialize(\DummyTest\SystemConfig::Get('xxx')); // 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();
}
@delith
Copy link
Author

delith commented Jul 16, 2014

HHVM version should of course be 3.1.0
There is no errors given even with full error reporting in HHVM.
In PHP you get a bunch of warnings about a non-static method being called statically.

@AlphaStream
Copy link
Contributor

Looking into this.

@AlphaStream
Copy link
Contributor

A shorter repro: http://3v4l.org/JZWtS

@darkain
Copy link

darkain commented Jun 3, 2015

Just as a note: This is still an issue a year later in HHVM 3.7.0

@lexidor lexidor mentioned this issue Sep 21, 2019
@lexidor
Copy link
Collaborator

lexidor commented May 14, 2020

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

This issue applies to running php code on hhvm. Hhvm nolonger supports running php code since hhvm version 4.0.0.

In Hack, this is simply not allowed (anymore).
Both the typechecker and the runtime agree.
The runtime throws a BadMethodCallException
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Non-static method DummyTest\SystemConfig::Set() cannot be called statically'
and the typechecker reports:

Typing[4090] No static method 'Get' in DummyTest\SystemConfig (did you mean instance method 'Set'?)
   --> file.php
 40 |             echo serialize(\DummyTest\SystemConfig::Get('xxx')); // Fails in PHP
    |                                                     ^^^
 46 |     class SystemConfig {
    |           ^^^^^^^^^^^^ Declaration of DummyTest\SystemConfig is here

The typechecker error could be better, because it thinks you might have meant to call ->Set() instead of ->Get(), but that is a suggestion.

@lexidor lexidor closed this as completed May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants