Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-17-2012, 06:27 PM   PM User | #1
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
A flexible GETTER Class.

NOTE: I will be updating this periodically to make it better. I am going to expand on it and make it a post and get class. The current version is 1.0
Here is a flexible GETTER class if anyone is interested. I am always open to improving it. Its pretty simple. Suggestions welcome.

Getter.php
PHP Code:
<?php
/* 
Author: Chris Hickingbottom
Date: 3/16/2012
Description: This class is designed to flexibly grab the variables in a url. 
Version: 1.0
*/
 
class Getter {
 
    private 
$getters = array();
 
    function 
__construct($gets) {
        foreach (
$gets as $get=>$default) {
            
$value = !empty($_GET[$get]) ? $_GET[$get] : $default;
            
$this->getters[$get] = $value;
        }
    }
 
    public function 
getGetter($get) {
        return 
$this->getters[$get];
    }
 
}
Example.php
PHP Code:
 
<?php
require_once('getter.php'); // include the file. 
 
 // set the variables you are searching for in an array 
 // the key being the name of the variable and the other being the default value if it is not set
$gets = array('firstName'=>false'lastName'=>false);
$getter = new Getter($gets); // initialize the class using the array of variables that you want
$carType=$getter->getGetter('carType'); // grab the specific infomation you want
$carModel=$getter->getGetter('carModel'); // grab the specific information you want
 
if(!$carType) {
    echo 
'False';    
} else {
    echo 
$carType;
}
echo 
"<br />";
if(!
$carModel) {
    echo 
'False';    
} else { 
    echo 
$carModel;
}
?>
__________________
Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
I always recommend the HEAD First series of books for learning a new coding language. ^_^

Last edited by Chris Hick; 03-18-2012 at 09:20 PM..
Chris Hick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.