As I mentioned in the introduction, PHP 5.3 will also let you work with namespace aliases in a very simple fashion. Indeed, this process is reduced to associating meaningful words with a certain namespace, which will require writing less, but more intuitive, code. To demonstrate a typical use of namespace aliases, I’m going to create for you a fictional scenario. In this example, two validation classes that share the same name are associated with different namespaces. The first of these namespaces will be used in the context of a blog application, while the second one will be associated with a content management system. So, based on this hypothetical situation, the use of namespace aliases could be exemplified in the following manner: use DataSanitizer::Blog::Validator as BlogValidator; use DataSanitizer::CMS::Validator as CMSValidator; try{ // create new instance of 'BlogValidator' class $blogValidator=new BlogValidator(); $blogValidator->validateString($_POST['fullname']); $blogValidator->validateInteger($_POST['age']); $blogValidator->validateEmail($_POST['email']);
// create new instance of 'CMSValidator' class $cmsValidator=new CMSValidator(); $cmsValidator->validateString($_POST['fullname']); $cmsValidator->validateInteger($_POST['age']); $cmsValidator->validateEmail($_POST['email']); } catch(Exception $e){ echo $e->getMessage(); exit(); } As you can see from the above example, two aliases, called “BlogValidator” and “CMSValidator” respectively, have been linked to a couple of namespaces, which should be easy for you to follow. These aliases are tied to the aforementioned validation classes, which logically share the same “Validator” name. Simpler to code and read, right? In this particular situation, I created a pair of validation objects to check the validity of user-supplied data, such as the user’s full name, age and email address. In this way you can see clearly how the objects in question can be utilized independently. So far, so good. Now that you hopefully grasped the logic that stands behind using namespace aliases, I think that it’s time to explore a few additional features regarding the use of namespaces with PHP 5, since there are a couple of useful things that I’d like you to learn. As you’ll recall, all of the examples developed in the previous tutorials (and the current one, actually) used classes that were defined along with the scripts that utilized them. However, there’s an important point that deserves a deeper analysis here. What happens if you want to include these classes via the “__autoload()” PHP magic function, as you’ve probably done before? Or, to put the question more plainly: how can a class that’s linked to a certain namespace be automatically included into a PHP script with “__autoload()”? Well, it’s a pretty simple process really, even when it requires using a slightly special syntax. Therefore, in the last section of this tutorial, I’ll be explaining in detail how to work with namespaces and the “__autoload()” magic function. And additionally, you’ll see how to link a simple PHP function to a specific namespace. As usual, to learn how all of these tasks will be accomplished, you’ll have to click on the link that appears below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|