php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20333 A return() in an include won't skip registering classes below
Submitted: 2002-11-09 22:56 UTC Modified: 2002-11-10 05:11 UTC
From: mgv at fx dot ro Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.2.3 OS: Linux
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: mgv at fx dot ro
New email:
PHP Version: OS:

 

 [2002-11-09 22:56 UTC] mgv at fx dot ro
When including a file which contains a class definition, performing a return() ABOVE the class definition will still result in the class being registered, if the class is completely new or if it extends a defined class. The same with functions. I believe this has to do with PHP 4 not caring where a function/class is declared in a script, so I expect PHP performs some pre-registration of these items before executing the actual code in the script. This behaviour is unexpected however when the user wants to perform an explicit return() before the class definition.

Although not really needed, here's an example demonstrating this behaviour:
-- myFile.php --
<?
   include("myLib.php");

   // This will be defined
   if (class_exists("myclass")) {
     echo("Original class IS defined!<br />");
   } else {
     echo("Original class IS NOT defined!<br />");
   }

   // This will be defined
   if (class_exists("myclass_ex")) {
     echo("Extended original class IS defined!<br />");
   } else {
     echo("Extended original class IS NOT defined!<br />");
   }

   // This will NOT be defined
   if (class_exists("yourclass_ex")) {
     echo("Extended non-existent class is defined!<br />");
   } else {
     echo("Extended non-existent class IS NOT defined!<br />");
   }

   // This will be defined
   if (function_exists("myfunc")) {
     echo("Function IS defined!<br />");
   } else {
     echo("Function IS NOT defined!<br />");
   }
?>

-- myLib.php --
<?
  return(1);

  // Code won't execute
  echo("Whoaa! return() didn't do a thing!");
  
  // Class will however be defined
  class myClass {}

  // Extended class of defined class will be defined
  class myClass_ex extends myClass {}

  // Extended class of undefined class will NOT be defined however
  class yourClass_ex extends yourClass {}

  // But a plain function will be defined
  function myFunc() {}
?>

You can see the code in action at http://bogdan.lanifex.com/PHP_include_test/myFile.php
(a highlight_file(__FILE__) is added in both files for clarity at that URL).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-10 05:11 UTC] derick@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
 [2017-12-04 11:50 UTC] phpdoc at mail dot my1 dot info
May I ask WHY this is not a bug? I looked around in the documentation and cannot find any explanation on why this is valid behavior.
 [2017-12-04 11:57 UTC] spam2 at rhsoft dot net
because PHP is not just a naive scripting language like bash where you can get in trouble when you edit a still running file - PHP *compiles* scripts and with opcache which is pretty standard these days even stores the so compiled bytecode

WHY?

* beause performance would suck otherwise
* because you would face errors only at runtime
* because it's natural to optimize programming languages
 [2017-12-04 12:41 UTC] phpdoc at mail dot my1 dot info
but why does compiling the PHP file make the class available even though it shouldnt be yet?

code that is not a class gets stopped properly so why not the class itself?

sorry if I dont really understand too much of what happens low level but that's specifically why I ask on why this happens.
 [2017-12-04 12:45 UTC] spam2 at rhsoft dot net
> sorry if I dont really understand too much of what happens low level
> but that's specifically why I ask on why this happens

you confuse a *bugtracker* with stackoverflow
 [2017-12-04 12:45 UTC] phpdoc at mail dot my1 dot info
also the reporter of this bug was directed to the manual, but WHERE is this behavior described. I checked the main page about classes (pretty much where that should be) but I didnt find it in the page's text.
 [2017-12-04 12:52 UTC] spam2 at rhsoft dot net
jesus that is a default paragrpah at clsoing bugreports which are not bugs and should go to whatever discussion forum - common sense - how do you expect PHP work with a class when it's not registered at compile time?

PHP is a *compiled language* for many years now

https://de.slideshare.net/jpauli/quick-tour-of-php-from-inside
 [2017-12-04 13:01 UTC] phpdoc at mail dot my1 dot info
but when a class is put inside an if-statement it doesnt load the class, even though no matter whether you use an if/else statement to stop the code or a return normally the results should be the same.
 [2017-12-04 13:03 UTC] spam2 at rhsoft dot net
what exactly did you not understand in the simple fact that this is a *bug tracker* and not a discussion forum about how PHP internally works?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC