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