Page 1 of 1

isset($foo), $_POST, and E_STRICT

Posted: Tue Feb 15, 2011 3:48 pm
by Celauran
As a rule, I declare all variables, even if that means initializing them to empty strings as is, I understand, good practice. I'm not really sure what to do about this with regards to form fields, though.

Code: Select all

if ($_POST['field'] == 'value')
generates 'Undefined index' notices.

Code: Select all

if (isset($_POST['field']) && $_POST['field'] == 'value')
seems a little needlessly verbose.

Do you just declare them all at the beginning of the page, or am I missing something else?

Re: isset($foo), $_POST, and E_STRICT

Posted: Tue Feb 15, 2011 4:08 pm
by John Cartwright
I will (as in most frameworks) usually represent my input as a request object (simplified), i.e.,

Code: Select all

class MyFramework_Request
{
   public function __construct($data) 
   {
      $this->_data = $data;
   }

   public function getParam($key, $defaultValue = null) 
   {
      return array_key_exists($key, $this->_params) ? $this->_params[$key] : $defaultValue; 
   }
}


$request = new MyFramework_Request($_POST);

if ($request->getParam('request') == 'value') {
   //do something
}