PHP 7.3.0 alpha 2 Released


In order to write future-proof code, it is recommended that you don't place many variables, functions or classes in the global namespace. This will prevent naming conflicts with 3rd 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.


namespace MyProject;

my_function() {


This still needs you to keep track of already used namespaces, but once you have decided on a namespace you will be using you can add all functions and classes 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 3rd 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 add a note

User Contributed Notes 4 notes

willian at gt44 dot com
4 years ago
I don't think it makes sense nowadays. We have namespaces now!
mestresan AT gmail DOT com
3 years ago
Even though nowadays we do have namespaces, for the cases in which the user chooses to not use namespaces theses tips should be followed.
kanone at rogers dot com
4 years ago
The javascript community has developed a strong cultural bias against adding anything to the global namespace.  (See e.g. Addy Osmani's article on Essential JavaScript Namespacing Patterns .)  Namespaces have been available in JS for many years and are ubiquitous.  There is also a great deal of freely available tutorial material for the interested reader.
earnie at users dot sourceforge dot net
4 years ago
@willian at gt44 dot com: What about clashes of the namespace?  It is still good advice to check for previously used names before assigning anything to the global namespace.
To Top