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!
blog comments powered by Disqus |
|
|
|
|
|
|
|