0

In the PHP manual it states that the Scope Resolution Operator(::) has the following purpose

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

Now I just came across a tutorial that was using the double colon like this:

class A {
  public function nice(){
    echo "hi";
  }
}

$A = new A;
A::nice();

and the output is actually

hi

But why does this not throw an error? The function nice is not a static method, nor is one overriding the method. Is it bad practice to use the double colon like that?

Adam
  • 25,960
  • 22
  • 158
  • 247

2 Answers2

3

It works for backwards compatibility with PHP 4, if your error reporting is on you will get warning:

Strict Standards: Non-static method A::nice() should not be called statically

EDIT Actually you will get error in recent versions, not warning.

Arman P.
  • 4,314
  • 2
  • 29
  • 47
  • http://stackoverflow.com/questions/4684454/error-message-strict-standards-non-static-method-should-not-be-called-staticall – TecBrat Jan 24 '17 at 15:45
1

It actually returns Deprecation notice and it's a bad practice:

PHP Deprecated: Non-static method A::nice() should not be called statically

It does not throw an error, because it's not referencing $this, so this method could be static anyway.

If you would try to reference $this, you will get following error:

Fatal error: Uncaught Error: Using $this when not in object context

jdrzejb
  • 116
  • 4