Req #67817 [NEW]: RFE: permit repeat declaration of the same function to skip rather than fail

From: Date: Sat, 09 Aug 2014 21:07:27 +0000
Subject: Req #67817 [NEW]: RFE: permit repeat declaration of the same function to skip rather than fail
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187053@lists.php.net to get a copy of this message
From: php at richardneill dot org Operating system: PHP version: 5.5.15 Package: Scripting Engine problem Bug Type: Feature/Change Request Bug description:RFE: permit repeat declaration of the same function to skip rather than fail Description: ------------ At the moment, PHP will not let us re-declare an existing function, but instead has the error: "Cannot redeclare FUNCNAME() (previously declared in FILE:LINE)" That's fine, and I understand why, unlike, say bash, we can't/shouldn't be able to change a function definition. However, it's rather a pain if you end up with multiple indirectly include()d files some of which get included more than once, and you forgot to wrap every single function definition with: if (!function_exists("FUNCNAME")){ function FUNCNAME(...){ ... }} My request is that, rather than throwing an error the second time, if PHP is encountering the *same* function in the *same* file, it should simply ignore the duplicate definition (and throw an E_NOTICE). This ought to be easy to implement (since PHP already knows the filename/line-num of the original definition at the time it reaches the duplicate), and the parser need merely ignore the duplicate. It should also not violate the principle of least-surprise, because if the function is identical, then even if it were re-defined, the programmer would expect it to do the same thing. So I believe this is an easy and uncontroversial win for the common case. Thank you for your consideration. Test script: --------------- ----- begin file inner.php --- <?php function a(){ echo "A\n"; } a(); ?> ------ end file -------------- ----- begin file main.php --- <?php include ("inner.php"); include ("inner.php"); ?> ------ end file -------------- Expected result: ---------------- This should print: A A Actual result: -------------- A PHP Fatal error: Cannot redeclare a() (previously declared in inner.php.3) in inner.php on line 4 -- Edit bug report at https://bugs.php.net/bug.php?id=67817&edit=1 --

« previous php.bugs (#187053) next »