Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
add comment

2 Answers

  1. You can't 'use' namespaces at all, sadly, only classes: http://www.php.net/manual/en/language.namespaces.importing.php. 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
add comment

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

for example instead of \ everything like

\DateTime()

I added under my namespace

namespace MyProject;
use DateTime;

...
DateTime();     //wihtout \
...
share|improve this answer
add comment

Your Answer

 
discard

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.