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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 12:01:29 2024 UTC