seagullJS
seagullJS

Reputation: 529

PHP function without static can still be called just like a static

I've noticed in PHP I can create a function without the static modifier but still call it as if it was a static function, just curious why this is allowed / what is actually happening.

class Foo {
   public function bar($i) {
       return $i + 1;
   }
}

Foo::bar(4); // 5

I would expect that the static modifier is required like:

class Foo {
   public static function bar($i) {
       return $i + 1;
   }
}

Foo::bar(4); // 5

Upvotes: 2

Views: 72

Answers (1)

pilsetnieks
pilsetnieks

Reputation: 10420

It's for compatibility with PHP4 where the described behavior was how it actually worked (there was no static keyword).

You should be getting an E_STRICT error, though, unless your error_reporting is set to not display E_STRICT.

Upvotes: 5

Related Questions