Req #67817 [Opn->Nab]: RFE: permit repeat declaration of the same function to skip rather than fail

From: Date: Sat, 09 Aug 2014 22:00:48 +0000
Subject: Req #67817 [Opn->Nab]: RFE: permit repeat declaration of the same function to skip rather than fail
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187054@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67817&edit=1 ID: 67817 Updated by: pajoye@php.net Reported by: php at richardneill dot org Summary: RFE: permit repeat declaration of the same function to skip rather than fail -Status: Open +Status: Not a bug Type: Feature/Change Request Package: Scripting Engine problem PHP Version: 5.5.15 Block user comment: N Private report: N New Comment: That's why include/require_once exists. Previous Comments: ------------------------------------------------------------------------ [2014-08-09 21:07:27] php at richardneill dot org 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 this bug report at https://bugs.php.net/bug.php?id=67817&edit=1

« previous php.bugs (#187054) next »