Home arrow PHP arrow Page 2 - Creating a Validation Helper Class

Review: a URL-handling helper class - PHP

Helper classes seem easy to build -- almost deceptively so. If you're building a helper class to assist with a crucial function, however, such as validation, the approach you need to take may seem a bit less straightforward. This fifth article in an eight-part series on building helper classes walks you through the process, complete with hands-on examples.

TABLE OF CONTENTS:
  1. Creating a Validation Helper Class
  2. Review: a URL-handling helper class
  3. Creating a validation class
  4. Validating alphabetic and alphanumeric values
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 17, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Just in case you haven't read the previous part of the series, where I explained how to build a simple URL-handling helper class, below I included the full source code for this class, along with some examples of how to use it to retrieve some of the values stored in the $_SERVER PHP array.

That being said, here’s the signature of the URL helper class:

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']);

}

 

}

Undeniably, the definition of the above “URLHelper” class is pretty easy to follow. It's comprised of a few basic static methods, which behave like simple proxies for the aforementioned $_SERVER PHP array.

That was the boring part. However, the following code samples should be a bit more interesting for you; they show how to use the helper class to generate some dynamic URLs. Here they are:

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

*/

From the previous examples, it’s clear to see that the methods of the URL helper class can be easily improved, or even better, it’s possible to add new ones that perform more useful tasks, such as generating breadcrumbs for a web page and calculating the base path of a given web site.

Despite the lack of these features, this sample helper class should give you a clear idea of how to take advantage of its functionality without having to spawn an instance of it.

Well, at this point you’ve surely recalled how to work with the previous URL helper, so the next thing that I’m going to do will consist of building a brand new class. This class will be responsible for validating common data, such as float and integer numbers, IP and email addresses, and so forth.

If you want to learn in depth how this new helper class will be developed, you’ll have to click on the link that appears below and read the following section.



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