Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we namespace functions in PHP?

We all know that namespacing your application code is a best practice and a PSR recommendation. The only thing PSRs take stance on is the whitespace and side-effects when it comes to functions and their declaration.

But what about functions which are not related to any single class? Should they be namespaced or left in the global namespace?

Some developers are saying that namespacing functions is bad practice but I wasn't able to grasp why.

From what I've learned/read and seen recommended regarding to namespacing functions:

  • NO: Don't use global functions without classes as they cannot be auto-loaded (this means creating supporting static classes or similar).
  • NO: Don't use global functions as they cannot be imported/used (fully qualified function name is always needed).
  • YES: Use namespaces for global functions to prevent collisions and to denote functions that are not part of PHP itself (you can safely assume you're calling the correct function).
  • NO: Don't use namespaces for global functions because the functions are not tied to a OO application structure anyways (has a point, but see previous point).

Are there best practices for global functions and namespacing them? Currently I use the application root namespace for global application related functions, but I keep wondering why some people strongly believe no namespacing should happen outside objects (classes, interfaces, traits, etc.)?

like image 853
ojrask Avatar asked Nov 16 '15 07:11

ojrask


People also ask

Should I use namespace PHP?

The main objective of namespaces is to prevent name collisions, more over they are used to group classes, methods. As you mentioned there are a lot of classes within a Laravel project so namespaces would have to be used to prevent collisions which will happen in big projects.

What is the purpose of namespaces in PHP?

A namespace is used to avoid conflicting definitions and introduce more flexibility and organization in the code base. Just like directories, namespace can contain a hierarchy know as subnamespaces. PHP uses the backslash as its namespace separator.

What is the purpose of namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

When were PHP namespaces added?

PHP Has Had Namespaces Since 5.3 Because the basic features of namespaces were added to PHP with 5.3, which came out all the way back in 2009.


1 Answers

Namespaces are a concept that allow you to impose a structure on smaller-scale constructs. With namespaces, you can put things together that belong together. It doesn't matter whether these things are classes or functions. If you have a free function that is only used together with two or three specific classes, I'd certainly put that function in the same namespace as the classes.

Whether or not you want to use free functions at all is a different question. Many OO languages such as Java and C# don't support them. But there are cases where a function just doesn't fit into a class. In these cases, I prefer creating free functions (in the appropriate namespace).

That being said, I'm pretty sure you will get both YES and NO answers here on SO as well. So discuss it with your team, and establish a guideline.

like image 93
theDmi Avatar answered Oct 14 '22 00:10

theDmi