2

We have recently been rewriting our PHP application's model code using OO. We have the classes done, and we included namespacing (in the form of Project\Level1\Level2 or whatever). Since this is an already-built application, there are a lot of classes. We are using an autoloader, for ease of loading the classes, which is great. But here's where the hassle comes in.

We have probably 300 pages strewn all across the application, and all of them require a PHP file at the very beginning (a sort of bootstrap script). The autoloader script is required in that bootstrap file.

Now, most of our pages will be using at least one but likely many of the classes. We know that we have two options here:

//either call the classes using qualified names
$person = new Project\Person;

//or include the "use" keyword on every page, so we can alias the classes for ease of use
use Project\Person as Person;
$person = new Person;

But the hassle is that we would really rather not have to do this on every single page in our application:

require_once('../php/bootstrap.php');
use Project\Person\ as Person,
    Project\Address\ as Address,
    Project\Group\ as Group
    Project\CustomField\ as CustomField;
$person = new Person;
$address = new Address;

We tried, for the heck of it, including the use keyword for all our classes in the bootstrap file, but according to the PHP manual, "Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules."

It just seems like including these sprawling use statements on every page of our application is creating more hassle than it's worth. And what if we are using 15 classes on one page? Then the use statement would be huge.

My question is this: is it too much hassle? Are we correct in using namespaces, or is it not all that helpful in PHP? And now knowing what we are trying to do, are we doing it right? Is there something we are missing? Should we be calling classes with fully qualified names all the time or something?

(this is my first question on stackoverflow, so please let me know if I need to do anything differently or better with my questions in the future)

2
  • Maybe you could look into the way Zend Framework namespaces classes? framework.zend.com/manual/en/zend.loader.autoloader.html Jun 4, 2012 at 21:35
  • @NickCaballero, thanks for the suggestion. We looked at Zend when we were rewriting, but decided against following their exact approach. With their underscore-solution, it essentially made each class name into a fully qualified name, which is quite burdensome. But I guess they probably did that to get past this dumb problem with PHP. :) Jun 5, 2012 at 13:55

2 Answers 2

1

While using vanilla Zend Framework Autoloader might not be they way you want to go, the Zend Loader does support custom autoloaders, that can respect namespaces specified in-line or dynamically at run-time. In your case, you are using use Project\Person\ as Person which can become cumbersome, as you already noticed.

Using namespaces with the autoloader will allow you to take advantage of auto loading using simple path traversal to organize classes, without having to append large paths just to get to the interesting parts.

So, instead of new Corp_Assets_Generic_Person_Address, you could keep the folder structure and use new Namespace_Person_Address.

Namespaces avoid clashes in class names. Specially in large applications implementing autoloaders that need to load the right class from a number of different libraries. Using shorter class names makes the code easier to read but may make it harder to maintain. Adding the use statements makes it easier to read and later maintain but then it would be cumbersome to write and refactor. Documenting the expected behavior of the autoloader and using custom autoloaders to make more readable class names would be my suggestion.

0

In php.ini add your file as a prepend_file

1
  • First off, I can't seem to get the auto_prepend_file command to work in my .htaccess file. Keeps giving me an "Internal Server Error." Maybe I'm doing something wrong there. And secondly, can you explain how your solution would help us please? Because according to what I've read about auto_prepend_file, it simply include()s a file at the beginning of each PHP request, which still doesn't help our problem (since "importing rules are per file basis"). If you have any further clarification, let me know please! Jun 5, 2012 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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