PHP 8.4.26 Released!

Tips

In order to write future-proof code, it is recommended not to place many variables, functions or classes in the global namespace. This will prevent naming conflicts with third-party code as well as possible future additions to the language.

One common way to prevent naming conflicts of functions and classes is to add them to their own dedicated namespace.

<?php

namespace MyProject;

function my_function()
{
    return true;
}

\MyProject\my_function();

?>

Keeping track of the namespaces already in use is still necessary, but once a namespace has been chosen, all functions and classes can be added to it without having to think about conflicts again.

It is considered best practice to limit the number of variables added to the global scope in order to prevent naming conflicts with third-party code.

Note: Variable scoping

Because of PHP's scoping rules variables defined inside functions and methods are not in the global scope and as such cannot conflict with other variables defined in the global scope.

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top