Home arrow PHP arrow Page 4 - Using Aliases and the Autoload Function with Namespaces in PHP 5

Autoloading classes and working with custom functions - PHP

Namespaces are an elegant approach to solving naming conflicts between the classes that are used by a PHP application. Indeed, it’s pretty common to suffer this problem during the development of a project that’s been charged to several programmers, and also when working with third-party libraries. This four-part series of articles, of which this is the last, shows you how to handle namespaces.

TABLE OF CONTENTS:
  1. Using Aliases and the Autoload Function with Namespaces in PHP 5
  2. Review: the use keyword
  3. Utilizing namespace aliases
  4. Autoloading classes and working with custom functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 03, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the previous section, the last two topics that I plan to discuss in this last part of this article are related to using namespaces with the “__autoload()” PHP magic function, and with regular functions as well.

First I’m going to show you a basic example of how to implement “__autoload()” when working with namespaced classes. Here’s a simple implementation for this magic function:

// implements the __autoload() with namespaced classes


function __autoload($className){

$className=str_replace('::','/',$className);

if(!file_exists($className.'.php')){

eval("class $className {}");

throw new Exception('Source class not found!');

}

require_once $className.'.php';

}


If you develop PHP applications that include source classes via the “__autoload()” function, then the above example should be pretty easy to understand. As you can see, I implemented this function in a fairly basic way, so it can include a class that’s been previously linked to a specific namespace.

The function, in this particular case, assumes that the class has been passed in the following form:

namespace::classname


Thus, it first replaces the :: characters with a backslash, and then finishes its execution by attempting to load the class in question. Pretty simple to understand, right?

Now that I have quickly explained how to include a class that has been tied to a namespace, please take a look at this final example. It shows how to define and use two simple PHP functions, where the first one is linked to a sample “MyOffice” namespace:

namespace MyOffice

// define function within the 'MyOffice' namespace

function displayNameSpace(){

return 'calling function from MyOffice namespace!';

}

// define function in the global namespace

function displayNameSpace(){

return 'calling function from the global namespace!';

}


echo MyOffice::displayNameSpace();

// displays 'calling function from MyOffice namespace!'


echo displayNameSpace();

// displays 'calling function from the global namespace!'


Indeed, this last example is very simple to follow, trust me. As you can see, first a trivial “displayNameSpace()” function is linked to a “MyOffice” namespace, and then another function that has the same name is declared in the global namespace. Since these functions are actually different, they’ll also generate distinct outputs.

The following code snippet clearly demonstrates this process:


echo MyOffice::displayNameSpace();

// displays 'calling function from MyOffice namespace!'


echo displayNameSpace();

// displays 'calling function from the global namespace!'

With this last code sample, I’m finishing this introduction to using namespaces with PHP 5. Please keep in mind that all of the code samples shown in this group of tutorials will work (hopefully) with PHP 5.3, and not with earlier versions.

Final thoughts

Sad but true, this is the end of the series. I hope that all the material included in these articles will help you grasp more quickly how to utilize namespaces with PHP 5. Indeed, they can be quite useful for solving naming conflicts between classes and functions, something that might occur incidentally when the development of a PHP application is tackled by several programmers.

See you in the next PHP development tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: