Tuesday, September 21, 2010

$_GET, $_POST, $_REQUEST, $_FILE Made Easy

This may not be a new invention or secret but am gonna share it any way:

INSTEAD OF ALL THESE;
$day = (isset($_REQUEST['birth_day'])) ?$_REQUEST['birth_day'] : NULL;

YOU CAN NOW WRITE JUST THIS;
$rq = new requesthandler;
$day = $rq->get('birth_day');


USING THIS CLASS;

class requesthandler {
function get( $data )
{
return (isset( $_GET[$data])) ? $_GET[$data] : NULL;
}

function post( $data )
{
return (isset( $_POSt[$data])) ? $_POST[$data] : NULL;
}

function request( $data )
{
return (isset( $_REQUEST[$data])) ? $_REQUEST[$data] : NULL;
}

/*
* @param:
* $data = $_FILES['file']['name']
* @ret:
* Array
*/
function files( $data )
{
return (isset( $data)) ? $data : NULL;
}
}


Always remember though to unset your classes to free the object from memory. its not that required but its a good practice when you are going on larger projects. use this to unset
unset( $rq );

No comments:

Post a Comment