Bug #20333 [Com]: A return() in an include won't skip registering classes below

From: Date: Mon, 04 Dec 2017 11:57:37 +0000
Subject: Bug #20333 [Com]: A return() in an include won't skip registering classes below
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212894@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=20333&edit=1 ID: 20333 Comment by: spam2 at rhsoft dot net Reported by: mgv at fx dot ro Summary: A return() in an include won't skip registering classes below Status: Not a bug Type: Bug Package: Class/Object related Operating System: Linux PHP Version: 4.2.3 Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2017-12-04 11:50:26] 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. ------------------------------------------------------------------------ [2002-11-10 05:11:55] 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 ------------------------------------------------------------------------ [2002-11-09 22:56:29] 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). ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=20333&edit=1

« previous php.bugs (#212894) next »