Justin
Justin

Reputation: 45320

PHP Calling A Method Statically; Even Though Its Not Defined As Static

How is this working? Shouldn't this throw an error, since I am trying to call a non static method statically? Basically, I've never instantiated an object of type something.

class Something {
   public function helloworld() {
       echo 'hello world';
   }
}

Something::helloworld();

Upvotes: 1

Views: 163

Answers (2)

Decent Dabbler
Decent Dabbler

Reputation: 22783

Put this at the top of your script:

error_reporting( E_ALL | E_STRICT ); // E_STRICT is important here
ini_set( 'display_errors', true );

... and see what happens then:

Strict Standards: Non-static method Something::helloworld() should not be called statically in [...]

Admittedly, it more of a notice than an error though. Your script will happily continue to run.

Upvotes: 1

Linus Kleen
Linus Kleen

Reputation: 34612

It would only give you an error, if within helloworld() you'd be using $this.

It's a type of PHP "WTF" resulting from missing specs that allows you to statically invoke a function not actually declared static.

Upvotes: 0

Related Questions