Parent :: method () - call a non-stationary method

I do not understand the concept of calling a parent method in PHP. The parent method is not static, but it is called statically - usually PHP generates an error / warning.

The question is, is this a quirk from PHP, or is it how it should be in OOP?

Taking an example from php.net:

<?php
class A {
    function example() {
        echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am B::example() and provide additional functionality.<br />\n";
        parent::example();
    }
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>

http://php.net/manual/en/keyword.parent.php

In PHP 5, calling non-static methods statically generates E_STRICT level warnings.

http://php.net/manual/en/language.oop5.static.php

+4
source share
2 answers

If you look at the definition of a static method , you will see:

  • , . - , .
  • , . - .

, PHP. , ++ .

, , . , JAVA , super.printMethod();, #, base.printMethod().

, PHP , parent->printMethod() .

+3

, , , , , .

, E_STRICT:

$b = new B;
$b::example();

+1

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


All Articles