Home arrow PHP arrow Page 4 - Enhancing a URL Handling Helper Class in PHP 5

Putting the new URL helper class in action - PHP

Are you a PHP developer wishing to find an approachable guide that walks you through building different kinds of helper classes in PHP 5? Then look no further, because this group of articles may be the material that you really need.

TABLE OF CONTENTS:
  1. Enhancing a URL Handling Helper Class in PHP 5
  2. Review: generating dynamic URLs simply
  3. Declaring the methods of the URL helper class static
  4. Putting the new URL helper class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 10, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: