DEV Community

Cover image for PHP, Functions, and Namespaces with the "use function" statement
Roberto B.
Roberto B.

Posted on • Edited on

PHP, Functions, and Namespaces with the "use function" statement

In PHP, namespaces play a crucial role in organizing code and avoiding naming collisions between different components of an application.
Namespaces allow developers to define a specific scope for classes, interfaces, and functions.
The use function statement within namespaces simplifies access to these functions and promotes cleaner and more organized code when using functions from external files.

This article will explore how to use the use function statement with namespaces in PHP and provide practical examples to illustrate its usefulness.

Creating Namespaces and External Functions

First, let's understand the process of creating namespaces and external functions. Consider we have an external file named math_functions.php with the following code:

// math_functions.php
namespace MyMath;

function add($num1, $num2) {
    return $num1 + $num2;
}

function subtract($num1, $num2) {
    return $num1 - $num2;
}
Enter fullscreen mode Exit fullscreen mode

Because we will use the namespaces in the math_functions.php file, we added the namespace MyMath statement.

For demonstration purposes, in the first example (Example 1), we will call functions without a namespace. For this reason, we will create an external file, math_functions_no_namespaces.php, that doesn't use namespaces.
The external files that define our functions:

// math_functions_no_namespaces.php

function add($num1, $num2)
{
    return $num1 + $num2;
}

function subtract($num1, $num2)
{
    return $num1 - $num2;
}

Enter fullscreen mode Exit fullscreen mode

Example 1: calling the function without a namespace

Before diving into namespaces, let's see how we use functions from an external file without namespaces.

The main file, where we are going to include the functions (without namespaces):

// example_1.php
require_once 'math_functions_no_namespaces.php';

$result = add(10, 5);
echo 'Result: '.$result; // Output: Result: 15
Enter fullscreen mode Exit fullscreen mode

In this example, we include the math_functions_no_namespaces.php file and use the add() function. However, as the project grows, multiple files may have functions with the same name, leading to conflicts. This is where namespaces come to the rescue.

Example 2: calling the function with namespaces via "use function"

Now, let's see how we can use namespaces and the use function statement to import functions from an external file:

// example_2.php
namespace MyApp;

require_once 'math_functions.php';

use function MyMath\add;

$result = add(10, 5);
echo "Result: " . $result; // Output: Result: 15
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined the namespace MyApp and imported the add() function from the MyMath namespace using the use function statement.

Example 3: calling the function specifying an alias, with Namespaces via "use function"

Now, let's see how we can define an alias for the imported function:

// example_3.php
namespace MyApp;

require_once 'math_functions.php';
use function MyMath\add as myAddFunction;

$result = myAddFunction(10, 5);
echo 'Result: '.$result; // Output: Result: 15
Enter fullscreen mode Exit fullscreen mode

By specifying the alias myAddFunction, we can now refer to the add() function within our namespace without conflicts.

Example 4: Using Multiple Functions with "use function"

The "use function" statement supports importing multiple functions at once:

// example_4.php
namespace MyApp;

require_once 'math_functions.php';

use function MyMath\{add, subtract};

$result1 = add(10, 5);
$result2 = subtract(10, 5);

echo "add() result: " . $result1 . PHP_EOL; // Output: add() result: 15
echo "subtract() result: " . $result2 . PHP_EOL; // Output: sub() result: 5
Enter fullscreen mode Exit fullscreen mode

In this example, we import both the add() and the subtract() functions from the MyMath namespace, allowing us to use them independently within the MyApp namespace.

You can define alias also for multiple imports in this way:

use function MyMath\{add as myAddFunction, subtract as mySubtractFunction};
Enter fullscreen mode Exit fullscreen mode

In this example, we import both the add and subtract functions from the MyMath namespace, each with its alias (myAddFunction and mySubtractFunction), allowing us to use them independently within the MyApp namespace.

Let's recap

Using the "use function" statement in conjunction with namespaces in PHP allows developers to organize and access functions from external files efficiently. Namespaces provide a clear and systematic approach to managing the scope of functions, classes, and other components, preventing naming conflicts and promoting code maintainability. By embracing namespaces and the "use function" statement, PHP developers can build cleaner, more modular, and scalable applications.

One more thing... autoloading

If you want to avoid requiring the external files manually in your code, you can use the autoload features provided by the composer.
If you are using composer, you can add the math_functions.php file in the autoload section of the composer.json file.

    "autoload": {
        "files": [
            "math_functions.php"
        ]
    },
Enter fullscreen mode Exit fullscreen mode

in the example_5.php file you can incloude only the autoload file:

// example_5.php
namespace MyApp;

require_once 'vendor/autoload.php';

use function MyMath\{add, subtract};

$result1 = add(10, 5);
$result2 = subtract(10, 5);

echo "add() result: " . $result1 . PHP_EOL; // Output: add() result: 15
echo "subtract() result: " . $result2 . PHP_EOL; // Output: sub() result: 5
Enter fullscreen mode Exit fullscreen mode

To be sure that your autoload file is updated run:

composer dumpautoload
Enter fullscreen mode Exit fullscreen mode

Top comments (0)