1

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();
2

2 Answers 2

1

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.

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

Comments

0

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.

3 Comments

IIRC, it's not a missing spec, it's just backwards compatibility to PHP 4.
...for which there are no specs.
Well, it's not written word by word, but if you read the PHP 5 docs it's clearly written that unspecified is like public. And in PHP 4 public functions could be statically called. So actually it's documented, even inside the static chapter. See as well: php.syntaxerrors.info/… - Just FYI, normally you should not care that much about PHP 4 any longer.

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.