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.

3
  • why not just use a plain old PHP object? No headaches with all the static and singleton nonsense you likely don't need. Plus, is testable. After all, it's called object oriented and not class oriented. Commented Mar 29, 2013 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. Commented Mar 29, 2013 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. Commented Mar 29, 2013 at 17:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

So you recommend using the first method, where I overwrite and call the parent static?
If you're using PHP5.3 or higher, yes. Also, it's override* not overwrite.
You will not get any warning. Warning is there only if you're calling non-static method statically.
Now i gotta cry cause god must have killed a lot of kittens in our project :(
No kittens dies because of that, it's the opposite that is utterly wrong!
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.