Mike Mike - 1 month ago 15
PHP Question

Calling static method non-statically

I have a child class that extends a class with only static methods. I would like to make this child class a singleton rather than static because the original developer really wanted a singleton but used static instead (obvious because every method in the static class calls the Init() function (basically a constructor)).

Most of the methods in the parent don't need to be overwritten in the child, but I would like to avoid having to write methods like this:

public function Load($id)
{
return parent::Load($id);
}


when I would prefer not to overwrite the method at all and just use:

$child->Load($id);


Is it possible to call a static method non-statically? Is it possible to extend a static object with an instance object? I know I can try it and it will likely work (PHP is very forgiving), but I don't know if there is anything I should be concerned about.

Answer
  • Can you inherit static methods?

Yes

  • Can you override static methods?

Yes, but only as of PHP 5.3 do they work as you would expect: http://www.php.net/manual/en/language.oop5.static.php (ie. self binds to the actual class and not the class it's defined in).

  • Is it possible to call a static method non-statically?

Yes, but you will get a warning, and God kills a kitten (don't do it).