0

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?

Turako
  • 614
  • 7
  • 11

1 Answers1

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