Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My company has created a modular PHP framework over the years, beginning long before PHP 5.3 added namespaces. Recently, we decided to start using namespaces in our library's new code, and we intend to namespace old code eventually. However, we have a few issues we are trying to work out to determine if adding namespaces to a pre-v5.3 codebase is just not a good idea:

  1. Can I namespace a class but also import (use) the global namespace so all global classes do not need a \ prefix?
  2. If we namespace an existing class in our library, how can we prevent all existing projects from breaking as soon as the framework upgrades? For example, can the library change the name of class ThemeEngine to \module\theme\ThemeEngine, but the existing websites call ThemeEngine without any code modification? If so, what would that code look like?
share|improve this question
  1. You can't 'use' namespaces at all, sadly, only classes: Edit: You can import classes from the global namespace, like they did with the ArrayObject in one of the examples.

  2. You could have a global section of use statements so that all pre-existing classes, which are in the global namespace as well, theoretically should find the classes just fine. Theoretically...

We are in the same boat as you, sort of. We have some old code that doesn't use namespaces, but does use the _ namespace faker (like Zend_Db_Table...) and we have some older code that doesn't even fake namespaces. We're probably going to take the same route, but we still need to test if it's possible at all. Good luck!

share|improve this answer

Yea what I did in the end was use - 'use' to import the individual classes that I needed

for example instead of adding '/' in front of everything like:


I added under my namespace

namespace MyProject;
use DateTime;

DateTime();     //without \
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.