note 111088 deleted from functions.user-defined by googleguy

From: Date: Sun, 13 Jan 2013 13:23:06 +0000
Subject: note 111088 deleted from functions.user-defined by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-193010@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- Note about nested functions (Example #3 Functions within functions). You must call the parent function before the nested function is loaded and thus becomes available for global usage. Furthermore, the nested function has no ties to its parent function or scope. <?php function test() { $a = 1; function dosomething() { echo "dosomething called $a\n"; } } test(); dosomething(); // notice: undefined variable: a but "dosomething called\n" still outputted test(); // suddenly fatal error ?> Furthermore, calling the parent function once more will result in a fatal "Cannot redeclare funcX()" error as demonstrated by the second "test()" call above. Consider looking into anon/closure/lambda (often functionally equivalent in phpland) instead: http://php.net/manual/en/functions.anonymous.php

« previous php.notes (#193010) next »