A common example of this is Laravel, specifically the reason is that the functions have names well simpleton and easy conflict with other scripts, as it may be that other scripts have already used it
The reason you don’t use namespaces for functions, which is possible, is so that such functions are accessible without calling them with namespace or use function
, this is to facilitate as they are a series of simple functions as said.
In the case of the specific Laravel to observe are actually a number of useful functions that in a future even PHP itself could implement in the core, my opinion
If you have a function that conflicts with an existing function, can be native or not, this will cause a Exception
, then this would be a side effect, as per the PSR-1 this is one of the side effects (side-effects) that we should avoid, ie together the statements should never be made:
- Change behaviors (e.g.:
ini_set
, error_reporting
)
- Send reply to output (output)
- Cause
Exception
That is, the functions can do this, but only at the moment they are called.
Example of side effects:
Imagine we have a global.php
which should contain the statements, 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";
Maybe to support older versions of PHP that do not have the function, for example,
hash_equals
which only has PHP 5.6 (and above) and support for those who use PHP 5.5 in some way. This can occur within the "system" itself, Wordpress has several versions... If you make a plugin there may or may not be a function, depending on the version you are using, then you use thefunction_exists
to know if it exists or not. These are the possible uses I see. P– Inkeliz
Got it, @Inkeliz, a lack of standardization in this case, can screw up everything, since in PHP function collision generates a Fatal Error.
– Wallace Maxters