php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #70800 Doc error on class definition order
Submitted: 2015-10-27 09:29 UTC Modified: -
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: joel at kociolek dot org Assigned:
Status: Open Package: Class/Object related
PHP Version: 5.6.14 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: joel at kociolek dot org
New email:
PHP Version: OS:

 

 [2015-10-27 09:29 UTC] joel at kociolek dot org
Description:
------------
---
From manual page: http://www.php.net/language.oop5.inheritance
---

(Followup to Bug #70783)

The doc states that 

"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."

This is not strictly true. There are cases that work and do not even emit a warning that allows classes to be defined after they are first used.


Test script:
---------------
All of these do work:

class B extends A {}
class A {}

class B extends A {}
class C extends B {}
class A {}

class C extends B {}
class A {}
class B extends A {}

class B extends A implements I { use T; }
class A {}
trait T {}
interface I {}


All of these produce fatal errors:

class C extends B {}
class B extends A {}
class A {}
(PHP Fatal error:  Class 'B' not found)

trait T {}
class B extends A {}
class A { use T; }
(PHP Fatal error:  Class 'A' not found)

interface I {}
class B extends A {}
class A implements I {}
(PHP Fatal error:  Class 'A' not found)

class A { use U; }
trait U { use T; }
trait T {}
(PHP Fatal error:  Trait 'U' not found)



Expected result:
----------------
The doc should use "should" instead of "must" and should ideally explain when defining classes before they are used is a must and when it is not.

"Unless autoloading is used, then classes should be defined before they are used. If a class extends another, then the parent class should be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces. As an exception to this rule, classes that do [... explanation as to when it is not obligatory...] may be used before they are defined"


Actual result:
--------------
Doc does not state which class definition order really works or not.


Patches

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC