Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created January 8, 2013 04:22
Show Gist options
  • Select an option

  • Save jehoshua02/4481121 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/4481121 to your computer and use it in GitHub Desktop.
Cuz we love seeing "Notice: Undefined index: ..." SOOOOO much!
<?php
require 'ValueHelper.php';
$get = new ValueHelper($_GET);
$page = $get->options->page->value(1);
$pageSize = $get->options->pageSize->value(10);
$searchTerm = $get->options->searchTerm->value();
<?php
class ValueHelper
{
/**
* Construct method
*
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Returns array item or null wrapped in another ValueHelper
*
* @param string $name
*
* @return ValueHelper
*/
public function __get($name)
{
if (is_array($this->value) && array_key_exists($name, $this->value)) {
$value = $this->value[$name];
} else {
$value = null;
}
return new ValueHelper($value);
}
/**
* Returns value, or default if value is null
*
* @param mixed $default
*
* @return mixed
*/
public function value($default = null)
{
return ($this->value === null) ? $default : $this->value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment