I'm started to learn Laravel 6.
In /vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
it have some functions without even namespace! So it's occupy global namespace. Is it normal?
Asked
Active
Viewed 272 times
0

Turako
- 614
- 7
- 11
-
I don't know if there is any official recommendation or source that it's not normal at all. – Maharramoff Jan 18 '20 at 21:22
1 Answers
2
Yes its normal and intended.
Your helpers.php
file should be used for global functions that just do not belong in any other category, like string manipulation, simple calculations, etc.
If you notice a pattern in your functions, you'll likely want to separate it into a class of its own.
So to avoid some errors, all of the helper functions are wrapped like this:
if(!function_exists('my_helper_func')) {
function my_helper_func($param) {
return $param;
}
}
However, you are right in the way that this can collide with other code if they apply the same stuff. Thats why Laravel has recently moved all the string and array helpers to a composer package, which now means that a global function like starts_with()
should be called like \Illuminate\Suppor\Str::startsWith()
.

Flame
- 6,663
- 3
- 33
- 53