Namespacing in PHP


Let’s say you have file foo.php with a single function:

<?php
// foo.php

function hello() {
    return 'hello from foo';
}

And hello() is called inside index.php:

<?php
// index.php

include 'foo.php';

echo hello();

When you run index.php in your command-line:

php index.php
hello from foo

But what if there’s a file bar.php that has a function with the same name?

<?php
// bar.php

function hello() {
    return 'hello from bar';
}

If you include the file in index.php:

<?php
// index.php

include 'foo.php';
include 'bar.php';

echo hello();

You will get an error when running index.php in the command-line:

php index.php
Fatal error: Cannot redeclare hello()

So how do we resolve this? With PHP namespaces.

So let’s namespace foo:

<?php
// foo.php

namespace foo;

function hello() {
    return 'hello from foo';
}

Then namespace bar:

<?php
// bar.php

namespace bar;

function hello() {
    return 'hello from bar';
}

And update index.php:

<?php
// index.php

include 'foo.php';
include 'bar.php';

echo foo\hello();
echo "\n"; // newline
echo bar\hello();

You should notice that the namespace name and function name are joined by a backslash.

Now when you run index.php, you should get the expected output:

php index.php
hello from foo
hello from bar


Please support this site and join our Discord!