Access to non-static class function in PHP

Why is this legal in PHP?

<?php
class Foo {
    public function test() {
        echo "hello\n";
    }
}

Foo::test();
?>

test () is a non-static function, but I can access it without an instance.

+3
source share
5 answers

I believe in this because of backward compatibility. In PHP4, you did not have a static keyword for methods (still looking for links, but for now this is all I found http://us2.php.net/manual/en/language.oop5.static.php ). Thus, PHP4 code can work without problems.

It is best to declare your static functions as such, and if you enable E_STRICT, you will see a notification about this.

error_reporting(E_ALL | E_STRICT);

: , , http://bugs.php.net/bug.php?id=34990 http://bugs.php.net/bug.php?id=47891.

+5

, , , , $this , .

+6

PHP:

E_STRICT.

+2

, E_STRICT. PHP .

0

, .

-2

Source: https://habr.com/ru/post/1723166/


All Articles