Home » Php » To PHP Namespace or not to PHP Namespace

To PHP Namespace or not to PHP Namespace

Posted by: admin April 23, 2020 Leave a comment


ok, I’m relatively new to PHP programming and have been plodding along quite unaware that it is possible to actually use namespaces in PHP as I can in c# etc.

It’s really ugly though as they have decided to use backslashes – why!?

Anyway, I am interested in other PHP programmers’ views on whether namespaces will catch on in PHP and whether I should begin to use them now?

How to&Answers:

Here are some solution attempts for those wondering about name space functionality, without php 5.3.

First, I must say, only prefixing worked for me, the other solutions below are sort of notes not to go that way.

Namespace functionality can be needed if, for example, one produces wordpress plugins, using your valued function library in them. Normally you cannot have multiple plugins with function declarations of same functions, it would cause fatal errors.

SOLUTION 1: prefix all your library functions with plugin_name_ or namespace_

 function str_len_utf8($str)


 function photoplugin_str_len_utf8($str)

Is just a matter of find-replace. Easy if the functions are already prefixed uniquely:

john_str_len_utf8() => photoplugin_john_str_len_utf8()

Good for the soul as well, supports the ego, ‘johns string library’ 😀

If you choose short and nice prefixes with some common sense, it works like a charm, as they say.

SOLUTION 2: enclose all your library/reeuse functions in a class.

class photopluginlib
     function str_len_utf8($a){ $a=$this->str_clean_utf8($a); ...}

All function calls in the class to the class are prefixed with $this->. Once this class is prepared, one can use it repeatedly without search-replace, changing the classname is enough. Usage:

$photopluginlib=new photopluginlibrary();

SOLUTION 3: enclose all your library/reeuse functions in a class, and use :: operator

class photopluginlib
     static function str_len_utf8($a){ $a=self::str_clean_utf8($a); ...}

All function declarations in the class have keyword static in front of function.

All function calls inside the class to the class are prefixed with self::

Once this class is prepared, one can use it without search-replace. The class name is the namespace, sort of. One only changes the class name and uses it as such.


No need to instantiate the class.

Looks nicer than $photopluginlib->str_len_utf8(), but I still prefer photoplugin_john_str_len_utf8()


  • all libraries need to go into 1 big library class declaration, you practically cannot add methods to classes later in php.
  • you cannot just declare new library functions here and there in different php files.
  • if multiple libraries are used, and they are using each other, they need to use self:: for function calls.
  • php5.2.17, common 2013, does not accept call_user_func('photopluginlib::functionname'), need to use call_user_func(Array('photopluginlib','functionname')) or inside class, call_user_func(Array(__CLASS__,'functionname')), which means code rewrite for wp add_action, preg_replace_callback etc.
  • needs php>=5
  • cannot mix code and function declarations, as in:

class photopluginlib{ add_filter('html','myfilter'); static function myfilter($html){return ''} }

  • For big code, it can quickly become a big complicated mind bender.

I prefer to just use prefixing until the real thing, namespaces, are widely available.

These solutions still mean one has to prefix all function uses. Namespace functionality would mean one can use functions without prefixing them, the prefix would be just once, at begin of the php file:

namespace photoplugin;

By the way, upgrading to php5.3 has an additional advantage, if you can choose not to code for php5.2 (still everywhere, year 2013) Php5.3 vs php 5.2.17 means instant speed increase. It looks like %30 etc speed increases are available, not considering the database side:


Hope this helps inspire some solutions to those reaching for namespaces.


Its use is already catching on. A couple of projects use it in their upcoming/beta versions. Most examples I’ve seen however use it like a cargo cult. Doctrine2 for example uses five or more nested namespaces (code smell), probably to provide a 1:1 mapping of namespace/class to the filesystem/directories. I guess the novelty makes PHP namespaces prone to unreasoned overuse.

Anyway, the syntax doesn’t help with readability that much. And it’s a big turn off for professional programmers. But if there is a serious use case in your project, just go for it. (Hypothetical naming conflicts are not the best reason.)


They will most likely not catch on until the core starts using them (in PHP 7 maybe possibly perhaps…), but using Python for a few months will show you that namespaces are AWESOME.


Unless all your code runs on your own servers, it’s too early to start using them as 5.3 is relatively new.

Other than that, I’m not sure if they will ever really catch on. Even classes took a long time to catch on with a large portion of the PHP programming population.


I would start learning how to use namespaces as soon as possible. Zend Framework 2.0 will use namespaces, which will mean that anyone using PHP 5.2 or lower will be out of luck. I use a virtual dedicated server, so I can control my PHP version. If you use cPanel/WHM, you can install PHP 5.3 very easily. If you are on shared hosting, it may be a little bit before you see 5.3 installed, although there are 5.3 adopters out there.


Namespace is not needing for PHP.

For example, in C# and Java, it is usual to use several libraries (specially standard one) and each libraries contain thousand of classes and definitions for each library. So, the chance of a conflict is high.

Instead, in PHP is different, we don’t need to load several libraries, at most a framework plus some separates .php files. How PHP works discourage to loads biggest libraries but to use a bare minimum. It is why the chance of a name conflict is pretty dim. Also, frameworks are seasoned to use a short prefix, for example Wp_User (WordPress).