Perhaps a bug in PHP, maybe a feature ...
... and maybe - a bug in my head tired by evening -))
Try it on PHP 5.3.1, version 5.3.2 did not have time to check.
The simplest code: We start, quite expectedly we receive "test". Which is logical. We use the LSB feature that has appeared recently; everything works correctly. OK, let's add some more code: Personally, my $ third-> test () method calls Fatal error: undefined class constant. Judging by the output of xdebug, the interpreter successfully reaches First :: init (), but does not find the First :: DIR constant, despite the explicit use of LSB. What to do? Panic and hell. All you need to do is add the keyword “static” to the definition of the First :: init () method. Here is such an interesting feature.
Carefully reading the manual, we can conclude that a static call to methods that are not explicitly defined as static can cause only a warning of the E_STRICT level.
“Calling non-static methods statically generates an E_STRICT level warning.”
As it turned out, there are situations when using the right keywords is not only useful, but also vital.
PS If this is my glitch associated with a misunderstanding of the language - I beg you to let me know, I want to know what I'm wrong about.
Try it on PHP 5.3.1, version 5.3.2 did not have time to check.
The simplest code: We start, quite expectedly we receive "test". Which is logical. We use the LSB feature that has appeared recently; everything works correctly. OK, let's add some more code: Personally, my $ third-> test () method calls Fatal error: undefined class constant. Judging by the output of xdebug, the interpreter successfully reaches First :: init (), but does not find the First :: DIR constant, despite the explicit use of LSB. What to do? Panic and hell. All you need to do is add the keyword “static” to the definition of the First :: init () method. Here is such an interesting feature.
abstract class First {
function init() {
echo static::DIR;
}
}
class Second extends First {
const DIR = 'test';
}
Second::init(); //ожидаем "test"
class Third {
function test() {
Second::init(); // Второй раз ожидаем "test"
}
}
$third = new Third();
$third->test();
Carefully reading the manual, we can conclude that a static call to methods that are not explicitly defined as static can cause only a warning of the E_STRICT level.
“Calling non-static methods statically generates an E_STRICT level warning.”
As it turned out, there are situations when using the right keywords is not only useful, but also vital.
PS If this is my glitch associated with a misunderstanding of the language - I beg you to let me know, I want to know what I'm wrong about.