Adrian World Design
  • Services
    • Website Consulting
    • Startup Consulting
    • Zend Development
  • Portfolio
    • My Framework
  • Knowledge
    • Web Standards
    • PHP bare-bones
    • Zend Framework
    • Zend Framework 2
    • Git - Github
    • Search Engine Optimization
    • Web Hosting
    • Linux
    • Microsoft Windows
    • Web Browsers
    • Mobile Devices
  • About
    • Business
    • Professionally
    • Personally
  • Contact
    • Contact Form
    • Phone
    • Email
    • Messaging

Knowledge Base Overview

Prefixes versus Namespaces

Knowledge ⇒ Zend Framework 2 ⇒ Fundamentals ⇒ Prefixes versus Namespaces
Tweet
Share on Tumblr

Created: Jul 19, 2012, 3:41:45 PM CDTLast updated: Aug 27, 2012, 10:16:39 AM CDT

As you may know, the Zend Framework 2 (ZF2) requires PHP 5.3 or higher and uses PHP namespaces. ZF2 also is missing all the require_once statements and relies on autoloading of all the classes in the framework; in turn autoloading depends on PHP namespaces. In other words you will need some sort of an autoloader even when you like to use ZF2 only as a library plus it won't hurt to have some understanding about namespaces.

For ZF2 alone this requires some changes to the autoloader class and methods to get all the classes in your project without any require_once or include_once statements and based on a class namespace.

However, this new autoloader class also supports older concepts like prefixes which have been the standard in the previous framework releases.

This document sheds some light on the difference between namespace and prefix and how do you have use these two parameters and concepts in the Zend autoloader.

StandardAutoloader

If you found this page via Google or other search engine I assume you looked at the StandardAutloader class and noticed the two different methods (well actually four) between namespace and prefix. Like for the methods registerNamespace() or registerPrefix().

The difference between these two is quite simple.

Prefix

If you are familiar with previous release of the Zend Framework then you should be well aware of the prefixes. All the class names are matching a directory and filename path via the underscore in its name. If you wanted a radio form element you were looking for the Radio class like this:

class Zend_Form_Element_Radio { ... }
// matching folder and filename Zend/Form/Element/Radio.php

For an autoloader to work it has to replace the underscore characters for the directory separators. Now with ZF2 and namespaces this has changed, of course.

Namespace

According to the example above we have a new name for the same class, like this:

namespace ZendFormElement;
class Radio { ... }
// also matching the same folder and filename like above

Our new autoloader has to basically match the namespace with the filename and in terms of Windows we could say we are good to go. However, the StandardLoader is a little bit more thorough here and changes all directory separators to forward slashes.

By the way, if you have Windows and are wondering about this forward slashes note that this is works very well on Windows for quite some time with PHP. You could say that the good ol' days of using DIRECTORY_SEPARATOR in fact are over.

Usage of StandardAutoloader

Finally a quick example of how you would use the StandardAutoloader class in a project where you use ZF2 as library and not the full MVC framework.

use ZendLoaderStandardAutoloader; // add to top of this file
...
// a require_once at least one more time
require_once PATH_TO_LIBRARY.'Zend/Loader/StandardAutoloader.php';
$loader = new StandardAutoloader();
$loader->registerNamespace('Zend' => PATH_TO_LIBRARY.'/Zend'));
// finally send namespaces and prefixes to the autoloader SPL
$loader->register();

If have more namespaces you can simple add them one by one or use the registerNamespaces() method and pass in an array with the names and paths.

If you have some "legacy" library or framework with underscores or as we now know prefixes us the registerPrefix() method. If you have more than one you'll see a registerPrefixes() method where you can add more than one as an array.

Finally you can also send an array when you initiate the class. The constructor uses the setOptions() which, finally, is another method to add your namespaces or prefixes to the autoloader class before registering.

blog comments powered by Disqus
Prev

Powered by FeedBurner Load our "Knowledge Base" feed in your RSS feeder

Follow us on Twitter
Follow us on Facebook
Follow us on LinkedIn
Follow us on Google+

All rights reserved, Adrian World Design ©2009–2022 Powered by Wejas Framework

Jump to Top