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

Review: generating dynamic URLs simply - 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

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.



 
 
>>> 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: