php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #27440 declare classes before usage
Submitted: 2004-02-29 18:35 UTC Modified: 2004-05-24 16:07 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: holliwell at gmx dot net Assigned: nlopess (profile)
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2004-02-29 (dev) OS: linux 2.4.20 RH8
Private report: No CVE-ID: None
 [2004-02-29 18:35 UTC] holliwell at gmx dot net
Description:
------------
Hi,

README.PHP4-TO-PHP5-THIN-CHANGES states that classes must be declared before used. That seems no longer to be true.

Reproduce code:
---------------
<?php
$test = new fubar();
$test->barfu();

class fubar {
  function barfu()
  {
  echo 'fubar';
  }
}
?>


Expected result:
----------------
Fatal error: Class 'fubar' not found in ....

Actual result:
--------------
fubar

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-29 18:42 UTC] derick@php.net
It is required if you use inheritance and interfaces, so I think it should stay like this. declaring classes after using them is IMO not really nice (and afaik it can hurt some performance), so I think we should keep advising not to do this.
 [2004-02-29 18:53 UTC] holliwell at gmx dot net
yup, so what about at least issue a warning with E_STRICT?
 [2004-03-09 19:10 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

In this case raising E_STRICT is not possible since at the 
time the error is generated the parser does not know if the 
class will be declared at a later point. This is just one 
of the PHP 5 <-> 4 incompatibilities that will need to be 
documented. 
 [2004-03-09 19:58 UTC] holliwell at gmx dot net
Iliaa, I did not get your point, sorry! 
1.) In php5 its possible to do $c = new someclass , before 
someclass is defined, _without_ throwing an error. 
2.) Wich error are you talking about: 
<qoute> 
since at the time the error is generated 
</qoute> 
3.) be gentle ;-) and do not mark this bogus,  at least a doc bug. 
Or change README.PHP4-TO-PHP5-THIN-CHANGES to reflect 
the current behaviour (which is not throwing an error in this 
case.) 
4.) AFAIK at some stage of php5 using a class before declaring 
was throwing an fatal error. This was changed because of bc.  
So why should it be not possible in E_STRICT mode to throw a 
warning? 
 
Kind regards 
Friedhelm
 [2004-04-19 17:02 UTC] nlopess@php.net
This is already stated in the migration chapter a long time ago.
 [2004-04-19 17:13 UTC] betz@php.net
Nuno, it seems you have following in mind: 
http://de3.php.net/manual/de/migration5.incompatible.php 
<qoute> 
The following example was valid in PHP 4, although it will 
produce a fatal error in PHP 5. 
 
Beispiel B-3. Classes must be declared before used 
<?php 
$test = new fubar(); 
$test->barfu(); 
 
class fubar { 
   function barfu() { 
       echo 'fubar'; 
   } 
} 
?> 
</qoute> 
 
No fatal error is produced, it is just the same thing as 
in PHP 4. It may be bad practice, but it is still possible 
without any error, not even a warning rinning E_STRICT 
enabled. 
 
At least declare before usage is good practice, but in 
some cases not mandatory. So the docs are wrong. 
 
Regards 
Friedhelm 
 [2004-04-19 17:36 UTC] nlopess@php.net
that information was true when I've wroten this appendice.

When I update the appendice, I'll fix this bug.
 [2004-05-24 16:07 UTC] nlopess@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:12 UTC] phpdocbot@php.net
Automatic comment on behalf of nlopess
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=15d583e6a38e7fcbe3aa14ff5625087334a4d0a1
Log: fix #27440 amd #27649 and some examples
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 05:01:27 2025 UTC