1

I'm terribly sorry for asking such a silly question, but I'm new to OOP and trying to figure out what static methods are used for. Here's an example PHP code:

class foo{
static function bar(){
//do something here
}
public function baz(){
//do something different
}
}

Documentation says:

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class

However, the both methods from the example above can be accessed from outside of the class with like:

foo::bar();
foo::baz();

It works (at least for PHP 5.3 that I'm using) without instantiation of the class foo. So, what's the point of using static methods, if the both ways work??

Forgive me once again for such a noob question. I've been really trying hard to find it out myself with no success. Thanks.

5
  • 1
    Your call to baz is technically invalid, even though it will work; you are calling it statically even though it is an instance-level method. Commented Oct 9, 2013 at 17:25
  • 1
    You can't to do it in >=5.4(Static call of the non-static method). You can using $this in the non static method and get error even in 5.3(Use $this in not object context) then call statically. Commented Oct 9, 2013 at 17:27
  • The point of a static method is so you can call a class level method without instantiating the class. baz() is an instance method and should be called on an instance of the class while bar() is a static method and should be called on the class itself. Commented Oct 9, 2013 at 17:32
  • Okay, as far as I understood, only static methods should be called with classname::method(), right? Just wondering... If calling non-static methods this way is invalid, why did it even work in <=PHP5.3? Commented Oct 9, 2013 at 17:36
  • 2
    You might want to read this, r.je/static-methods-bad-practice.html Commented Oct 9, 2013 at 17:54

2 Answers 2

3

Static methods are globally available tools (helpers) and are often overused. Static methods are death to testability and should be avoided almost completely.

Their advantage as well as disadvantage is that they live in the global scope, you can call them from anywhere, which makes them code smells in most cases because they break encapsulation.

On language level, as Kolink mentions, things like mysqli:real_escape_string() make sense of course, but on application level, you usually don't want to pollute your global scope and break encapsulation like that. You'd rather think of what tools you really need and where and inject them bundled into meaningful groups (classes).

Your foo::baz() raises a warning in strict mode (which should always be on):

Strict standards: Non-static method foo:baz() should not be called statically in yourfile.php on line x

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

Comments

2

Generally they can be used for "helper" functions, such as mysqli::real_escape_string(), where the function is relevant to the class but not locked to any specific instance of it.

1 Comment

Understood. But what would happen if real_escape_string() method was not declared static like method baz() in my example? Why method baz() can still be accessed from outside of the class?

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.