In PHP is wrong to cast $_REQUEST to an object to manage it more easily?
$request = (object) $_REQUEST;
if(isset($request->submit) && isset($request->text) && !empty($request->text))
{
// Do stuff
}
EDIT: i mean any unexpected side effect?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would say there’s nothing wrong with this except obviously the little added time you’ll get. What I would do though is run the $_REQUEST array through a cleaner function to validate and clean up the variables. This way you can always know that if your using the object version of the $_REQEUST that it’s safe to use.
Update
I would go as far as saying this is probably best practice in the world of modern web applications. I mean look at the MVC frameworks you use (Zend, CodeIgniter, CakePHP etc) they all clean the $_REQUEST, $_GET, $_POST, $_SERVER etc and convert them to objects.
Remeber that the overhead on something like this would be so minimal you wouldn’t even notice it. The time PHP takes to parse and output a page is so small, usually smaller than a DOM request for one image.