Bug #68936 [Opn->Nab]: If class uses trait, it is not loaded after any global code that precedes it
Edit report at https://bugs.php.net/bug.php?id=68936&edit=1
ID: 68936
Updated by: requinix@php.net
Reported by: jevgenijs at zini dot lv
Summary: If class uses trait, it is not loaded after any
global code that precedes it
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Linux
PHP Version: 5.5.21
Block user comment: N
Private report: N
New Comment:
That's right.
I'm not actually aware of any place in the documentation that says classes are available to use
before they are defined - same file or not. In fact, the closest I can get is the note on the Object
Inheritance page which says the opposite:
Unless autoloading is used, then classes must be defined before they are used.
If a class extends another, then the parent class must be declared before the
child class structure. This rule applies to classes that inherit other classes
and interfaces.
http://php.net/manual/en/language.oop5.inheritance.php
Yes, I know that in practice simple classes can be defined automatically by merely being contained
in a file, but as far as I can tell that's unofficial and undocumented behavior so you
shouldn't depend on that. And here is one place where that won't work.
Previous Comments:
------------------------------------------------------------------------
[2015-01-28 17:44:15] jevgenijs at zini dot lv
Description:
------------
Normally, if file c.php contains class definition, the class is loaded before any global code in
file c.php is executed. However, it is not true if class uses trait.
If I remove "use C;" from the example below, the code passes.
Test script:
---------------
==== ./a.php ====
<?php
require_once 'c.php';
==== ./c.php ====
<?php
require_once 'b.php';
trait C {}
class A {
use C;
}
==== ./b.php ====
<?php
require_once 'c.php';
class B extends A{}
php -f a.php
Expected result:
----------------
no error
Actual result:
--------------
PHP Fatal error: Class 'A' not found in /www/htdocs/www.zini.lv/develjonas/tmp/b.php on
line 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68936&edit=1
Thread (2 messages)