Why do libraries use "! function_exists ('function') "whenever there is a declaration of a function?

5

I have already used several PHP libraries and I have noticed that they always use the same statement whenever there is a declaration of a new function.

if (! function_exists('funcao')) {
    function funcao($arg) {

    }
}

That is, the function should only be declared if it does not exist.

But why such a practice? Why worry about whether the function exists or not?

If this is to avoid name collision, with new PHP versions that implement namespace , which also work for functions, is there a need to make such a statement?

    
asked by anonymous 06.03.2017 / 17:09

3 answers

5

A common example of this is Laravel, specifically the reason is that the functions have very simple names and easy conflict with other scripts, as other scripts may already have used it

The reason for not using namespaces for functions, what is possible, is for such functions to be accessible without having to call them with namespace or use function , this is to facilitate, since they are a series of simple functions as said .

  

In the case of the specific Laravel itself, it is a series of useful functions that in the future PHP itself could implement in the core,

If you have a function that conflicts with an existing function, whether it is native or not, this will cause a Exception , then this would be a side effect , as PSR-1 this is one of the side effects that we should avoid, ie statements together should never be done:

  • Change behaviors (ex: ini_set , error_reporting )
  • Send response to output
  • Cause Exception

In other words, functions can do this, but only when they are called.

Example of side effects:

Imagine that we have a global.php that should contain the declarations, it will be included in all files:

<?php

//Pode causar um efeito colateral se já existir uma função com mesmo nome
function view()
{
   //Algo aqui....
}

//Pode causar efeito colateral acaso file.php não exista
include 'file.php';

//Causa efeito colateral, pois envia conteúdo para a saída
echo "<html>\n";

Example of declaration and use without side effect:

global.php:

<?php

//Evita conflito com outros scripts
if (!function_exists('view')) {
    function view()
    {
        //Algo aqui....
    }
}

3rdparty.php:

Third-party file, which you are using:

<?php

function foo() { ... }
function view() { ... }
function bar() { ... }

index.php:

<?php

include '3rdparty.php';
include 'global.php';
include 'file.php';

echo "<html>\n";
    
06.03.2017 / 17:32
1

Normally this type of verification is more useful for making a polyfill, that is, having a function that does something if it does not exist.

One common use I see for these cases is multi-byte support. Let's look at the str_len function to get a better idea.

We have three extensions that provide a function of counting characters in a string:

Note that the native php function does not correctly treat characters that are in some encodings (such as UTF-8) because the characters can occupy more than 1 byte.

For this reason, the most correct is to use the counting function of mb or iconv. But which one is available?

With the following code, we can easily declare a function according to the extensions that are available in php:

if(function_exists('iconv_strlen')) {
  function meu_strlen($entrada) {
    return iconv_strlen($entrada);
  }
} else if (function_exists('mb_strlen') {
  function meu_strlen($entrada) {
    return mb_strlen($entrada);
  }
} else {
  /* Tudo falhou, vamos usar a função do PHP mesmo :( */
  function meu_strlen($entrada) {
    return strlen($entrada);
  }
}
    
06.03.2017 / 17:35
-2

This makes such a function only set if it does not already exist. Interesting if you need to overwrite some of them, you define yours, and then the framework "question": This function here that I will define now, has the user done it before? If so, I will not redefine it, I'll let it use what it did. Otherwise, I'll define it

    
06.03.2017 / 20:42