I'm studying PHP and reading the documentation I came across this example:
<?php
$makefoo = true;
/* Nos nao podemos chamar foo() daqui
porque ela ainda não existe,
mas nos podemos chamar bar() */
bar();
if ($makefoo) {
function foo()
{
echo "Eu não existo até que o programa passe por aqui.\n";
}
}
/* Agora nos podemos chamar foo()
porque $makefoo foi avaliado como true */
if ($makefoo) foo();
function bar()
{
echo "Eu existo imediatamente desde o programa começar.\n";
}
?>
I understand that the function will only exist if the condition is evaluated as true, but it has given me some doubts, because what I believe to be normal (from what I see in scripts ) is define functions and decide which one to invoke from conditional tests, but in this example of the documentation the test will define whether the function will be declared or not, preventing it from being invoked later if not, which leads me to some doubts: / p>
Is it considered a good practice? Since the code I believe becomes more complex, as the rest of my code must be prepared to work perfectly even with the absence of this function.
Would you have any examples of application? I'm the type who understands things most clearly by some example.