Certainly, the best way to understand how to use the URL helper class now that most of its methods have been defined static is by means of some concrete examples that show how to exploit its functionality without having to deal with any instances of it. So, based on this simple concept, below I wrote a few code samples that demonstrate how to use the helper to abstract the access to the elements of the $_SERVER PHP superblobal array. The examples in question look like this: echo URLHelper::get_document_root(); /* // displays the following C:/Program Files/Apache Group/Apache2/htdocs */
echo URLHelper::get_request_method(); /* displays the following get */
echo URLHelper::get_query_string(); /* displays the following fname=Alejandro&lname=Gervasio */
echo URLHelper::get_request_uri(); /* displays the following /helpers/helper_example.php */
echo URLHelper::get_script_filename(); /* displays the following C:/Program Files/Apache Group/Apache2/htdocs/helpers/helper_example.php */
echo URLHelper::get_script_url(); /* displays the following /helpers/helper_example.php */ There you have it. As shown by the above group of code samples, generating dynamic URLs with the helper class is an extremely simple process, since it only requires calling the appropriate method statically and nothing else. It's that easy, really. Of course, this example URL helper class is only that: an example that needs to incorporate much more functionality. Nonetheless, it should be pretty useful for illustrating the basic concepts that surround the development of helper classes in PHP 5. Now that you've hopefully grasped the right pointers, test your own programming skill and try to develop a full-featured URL helper class. The experience will be not only instructive, but you'll have a great time doing it! Final thoughts That's all for the moment. Over this fourth chapter of the series I hopefully demonstrated how to make better use of the previous URL helper class by declaring most of its methods static. As you saw for yourself, this subtle modification in the source code of the class can produce a big impact in the way that it's used to generate dynamic URLs. Moving forward, in the following article I'm going to continue this round up on building helper classes in PHP 5 by adding a new one to the list. Undoubtedly, one of the most common tasks that must be talked about when developing PHP applications is validating user-supplied input. To make this process as painless as possible, in the forthcoming tutorial I'm going to build a data validation helper, which will use the functionality of the PHP filter extension to perform the pertinent validation processes. Don't miss the next tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|