Functions defined inside conditional blocks

0

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.

    
asked by anonymous 25.09.2018 / 01:06

1 answer

2

For those who accompany me, I know that I am a critic of good practices, and of course, bad ones as well. I like to understand motivations, to have the foundation to do right. And I talk a lot of context. Everything may be valid in the right context. And it may be that the valid context is a nonsense.

The question does not show the reference to give context because the code was done like this. You may have a reason. And those who did should usually justify. In programming we must do only what we can justify. We can not do it because someone did it before, it has to be always because it needs to. And it can not be invented justification, forced, it has to be a fair use even.

What problem does this solve? I can not see a reason for this. So if those who use can not show the advantage is not good practice.

I suspect, and only suspect it can be an optimization because it is an interpreted language, and silly, even because if language needs this and you benefit fundamentally by doing so it is the wrong tool. And it has cases that will be worse, especially if you are using some tool that will cache the compiled code.

It could also be defensive programming, but in the wrong way. The code is trying to solve a possible problem, but this is programming error, and programming error we fix in the code and does not try to get around the implementation.

Finally it can be some "clever trick", which is called clever code to be able to do some very flexible thing, but it seems to me to be gambiarra. It might be a way to create a template , but this way I would never use it.

If at least the function has local scope, but will have global scope equal to the others.

In fact the code becomes complex and you can not see advantage. I actually consider it a mistake for language to allow this kind of thing.

Create the functions you need and decide at some point whether to call them, but not the definition. Declarations and definitions should never be conditional.

    
25.09.2018 / 01:40