7

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.

Mike
  • 1,718
  • 3
  • 30
  • 58
  • why not just use a plain old PHP object? No headaches with all the static and singleton nonsense you likely [don't need](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323). Plus, is testable. After all, it's called *object* oriented and not class oriented. – Gordon Mar 29 '13 at 16:19
  • Singleton is a design pattern. It uses a "plain old" php object, it just means it has a private constructor and a static GetInstance method. It guarantees that only one instance of an object ever exists, but since it has a constructor, you don't have to initialize variables in every method. – Mike Mar 29 '13 at 16:32
  • I know what a Singleton is :) What I am saying is: you don't need it. See the link in my initial comment. You are making your life miserable with statics and singletons. – Gordon Mar 29 '13 at 17:54

2 Answers2

12
  • 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 will lose $this. You don't get a warning (yet) but there also isn't really a reason to call it the wrong way.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
10

Two part answer.

First, about the titular question: calling a static method non-statically is perfectly fine; @SamDark's comment is correct. It does not produce a warning, nor does it cause any kitten murdering. Try it:

<?php

class test {
    public static function staticwarnings(){
        echo "YOU ARE (statically) WARNED!\n";
    }
}

error_reporting(E_ALL);

$test = new test();
echo "\n\ncalling static non-statically\n";
$test->staticwarnings();

If you had an instance reference, $this, in that static method, then you would get a fatal error. But that is true regardless of how you call it.

Once again, there isn't a warning, nor any kitten killed.


Second part of the answer:

Calling an overridden parent function from an overriding child class requires something called "scope resolution". What the OP is doing in their method is NOT calling a static method. (Or at least, it doesn't have to be; we can't see the parent implementation). The point is, using the parent keyword is not a static call. Using the :: operator on an explicit parent class name is also not a static call, if it is used from an extending class.

Why is that documentation link so strangely named? It's literally Hebrew. If you've ever run into an error related to it, you might have observed the delightfully-named parser error code T_PAAMAYIM_NEKUDOTAYIM.

sanmai
  • 29,083
  • 12
  • 64
  • 76
Seth Battin
  • 2,811
  • 22
  • 36