# Make Global Functions Available in Laravel and Symfony with Composer Autoloading

> Author: [Oussama Ghaieb](https://oussama.ghaieb.com/about-me)
> Canonical: https://oussama.ghaieb.com/posts/make-global-functions-available-in-laravel-and-symfony-with-composer-autoloading

# <span class="hover:bg-yellow-100">Make Global Functions Available in Laravel and Symfony with Composer Autoloading</span>

When developing applications with frameworks like Laravel or Symfony, reusability and organization are essential. One powerful tool that facilitates this is Composer's autoloading feature. In this post, we’ll explore how to create a `helpers.php` file, autoload it with Composer, and use global functions across your application while maintaining clean and reusable code.

### Why Autoloading Matters
Autoloading ensures that classes, interfaces, and functions are loaded only when needed, eliminating the hassle of manually requiring files. With Composer's autoloading, you can also include utility files like a `helpers.php` file, making their functions globally accessible.

### Step-by-Step: Setting Up a `helpers.php` File
Let’s create a `helpers.php` file and autoload it in a Laravel or Symfony project.

1. **Create the File**
   Start by creating a `helpers.php` file in a directory, such as `app/Support`:
   ```php
   if (! function_exists('format_date')) {
       /**
        * Format a date to a readable string.
        *
        * @param string $date
        * @return string
        */
       function format_date($date)
       {
           return date('F j, Y', strtotime($date));
       }
   }
   ```

   The `if (! function_exists(...` block ensures there are no conflicts if the function already exists.

2. **Configure Autoloading**
   Open your `composer.json` file and add the `helpers.php` file under the `files` section:
   ```json
   {
       "autoload": {
           "files": [
               "app/Support/helpers.php"
           ]
       }
   }
   ```

3. **Update Autoloader**
   Run the following command to regenerate Composer’s autoload files:
   ```bash
   composer dump-autoload
   ```

4. **Use the Function**
   Now you can call `format_date()` from anywhere in your application:
   ```php
   echo format_date('2025-01-01'); // Outputs: January 1, 2025
   ```

### Benefits of Using Composer Autoloading
- **Reusability:** Define utility functions once and reuse them across your project.
- **Maintainability:** Centralize helper functions in one file, making them easier to manage and update.
- **Framework Agnostic:** While we’ve mentioned Laravel and Symfony, this approach works in any Composer-based PHP project.

### Conclusion
Composer’s autoloading feature is a game-changer for managing global functions in a PHP application. By setting up a `helpers.php` file, you can streamline your development process, enhance code reusability, and maintain a clean codebase. Whether you're working on a Laravel project, a Symfony application, or any other PHP project, Composer’s flexibility ensures a smoother development experience.
