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";