Update doc-en/language/functions.xml#5599
Conversation
|
Hi @Abrifq The given example tries to explain conditional functions. So to specify, functions that are within a conditional statement. The example above is almost identical to this change. Therefor I don't think anything is wrong with the current example. /* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
if (function_exists('foo')) {
foo();
} else {
echo "Function foo does not exist (yet).\n";
} |
Technically a more correct term could be "conditionally defined functions", as the condition has to be met only on where the definition is being made.
After defining (Though, I would reject a PR in heartbeat that randomly defines a function without saving the result of the random condition. This is about syntatically valid behavior vs. morally valid behavior, like the condition being a constant) $makefoo=false;
if($makefoo){
function foo(){ return "foo fighters!!"; }
}
$makefoo=true;
if(function_exists('foo') /* == false */) foo();
if($makefoo /* == true */) foo();
It is identical in the sense that the "inner" functions get defined in another block scope, whether in an if condition or function body. However I'd like to believe the proper use case for conditionally defined functions to be something like compiler definitions and the use case for functions defined in another functions to be something like lazy evaluated modules. In both cases though, the consumer code shouldn't check the environment variable or call the TL;DR: the condition being met at call time itself doesn't guarantee the function is available, therefore the user should ALWAYS check before attempting to call. |
Updated the part explaining when a function becomes available.
It used to check for the boolean, which was always true since the code defines it in the first line, but the boolean itself does not guarantee the function was available.
The example code now checks if the function exists again, so it should make more sense.