Enhancing a URL Handling Helper Class in PHP 5

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.

Welcome to the fourth part of a series on building helpers in PHP 5. Comprised of eight tutorials, this series shows how to create some of the most common types of helper classes found in many popular PHP frameworks, and it complements the corresponding theoretical concepts with numerous code samples.

Having already outlined the goal of this set of articles, now it’s time to review the topics that were treated in the last one, so you can see how they’ll link with the subject of this fourth tutorial. In that specific article I went through the development of a basic URL-handling helper class, whose methods behaved in most cases as simple wrappers for some of the values available in the native $_SERVER superblobal PHP array.

While it’s fair to say that the structure and logic implemented by this helper class was rather rudimentary, it’s worth stressing that it demonstrated how to build a URL helper class in PHP 5 in a straightforward fashion.

Of course, the class in question is susceptible to being enhanced, and that’s precisely what I’m going to do in the next few lines. You might be wondering how. Well, as you’ll possibly recall, all of the methods of the class were called initially inside the object context, that is dynamically, which may lead to the creation of an unnecessary instance of the helper class.

So, to solve this issue quickly, in this chapter of the series I’m going to redefine the signature of the previous URL helper class by declaring all of its methods static. Thus the question now is: are you ready to learn the full details of how this will be accomplished? Then don’t waste more time; start reading right now!

{mospagebreak title=Review: generating dynamic URLs simply}

It’s possible that you still haven’t had the chance to read the preceding part of the series, where I explained how to create a basic URL helper class with PHP 5. Therefore, below I reintroduced the complete source code of this class, along with some examples that show how to work with it.

First, here’s the signature of the helper class:

class URLHelper

{

// constructor not implemented

public function __construct(){}

 

// get web server root

public function get_document_root()

{

return $_SERVER['DOCUMENT_ROOT'];

}

 

// get script URL

public function get_script_url()

{

return $_SERVER['PHP_SELF'];

 }

 

// get script query string

public function get_query_string()

{

return $_SERVER['QUERY_STRING'];

}

 

// get request URI

public function get_request_uri()

{

return $_SERVER['REQUEST_URI'];

}

 

// get script file name

public function get_script_filename()

{

return $_SERVER['SCRIPT_FILENAME'];

}

 

// get request method

public function get_request_method()

{

return strtolower($_SERVER['REQUEST_METHOD']);

 }

}

Having already listed the complete source code corresponding to the previous URL helper class, here is the group of examples that demonstrate how to use it to retrieve indirectly some of the values available in the $_SERVER PHP superblobal array:

$urlhelper = new URLHelper();

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

*/

Undeniably, understanding how to work with the methods provided by the prior URL helper class is really simple, since they act like proxies for obtaining the values stored on the $_SERVER array. Period.

However, despite the fact that it’s feasible to add more complex methods to the class and improve the existing ones, there’s one aspect of it that definitely should be fixed as soon as possible. Yes, as I mentioned in the introduction, the helper in its current version permits you to call its methods outside and inside the object context indistinctly, which at first sight seems to be a good thing.

However, this feature is actually a bit tricky, since if the methods are invoked dynamically, it implies that an instance of the helper class had to be created in first place, right?

But, as you know, this instantiation of the class is completely unnecessary, and avoiding it is as simple as declaring all of its methods (except for the constructor) static. Therefore, in the course of the following segment, I’m going to alter the signature of the helper class to turn its dynamic methods into static ones.

To learn the full details of this redefinition process, click on the link below and keep reading.

{mospagebreak title=Declaring the methods of the URL helper class static}

As I anticipated in the section that you just read, the next step I’m going to take to enhance the previous URL helper class will consist of declaring its implemented methods static, which makes it easy to call them without having to spawn an instance of the class in question.

That being said, please take a look at the modified version of the URL helper class, which has been included below. Here it is:

class URLHelper

{

// constructor not implemented

public function __construct(){}

 

// get web server root

public static function get_document_root()

{

return $_SERVER['DOCUMENT_ROOT'];

}

 

// get script URL

public static function get_script_url()

{

return $_SERVER['PHP_SELF'];

}

 

// get script query string

public static function get_query_string()

{

return $_SERVER['QUERY_STRING'];

}

 

// get request URI

public static function get_request_uri()

{

return $_SERVER['REQUEST_URI'];

}

 

// get script file name

public static function get_script_filename()

{

return $_SERVER['SCRIPT_FILENAME'];

}

 

// get request method

public static function get_request_method()

{

return strtolower($_SERVER['REQUEST_METHOD']);

}

 

}

Naturally, if you’re anything like me, then it’s possible that you didn’t notice any big differences in the way that the above helper class has been defined. However, in this case the class’s methods now have been declared static, in this way introducing the aforementioned benefits.

Since the modification of the signature of the helper class should be pretty easy for you to follow, it’s time to develop a few illustrative examples to demonstrate how to take advantage of its functionality by calling its methods out of the object scope.

Want to see how these examples will be created? Then read the following segment. We’re almost finished!

{mospagebreak title=Putting the new URL helper class in action}

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!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan