Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active October 1, 2015 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/1987336 to your computer and use it in GitHub Desktop.
Save xeoncross/1987336 to your computer and use it in GitHub Desktop.
View Error Handling For Missing Values
<?php
// Sets the error handler to check the current class for the value if it's missing
// http://stackoverflow.com/a/2669273/99923
error_reporting(E_ALL);
ini_set("display_errors", 0);
class View {
function display($file, $values)
{
set_error_handler(array($this, '__get'), E_NOTICE);
extract($values);
include($file);
restore_error_handler(); // error handlers are stacked with set_error_handler()
}
function __get($vaule)
{
echo '[Unknown]';
}
}
$View = new View;
$values = array('user' => 'Tom', 'email' => 'email@host.com');
$View->display('/filename.php', $values);
?>
filename.php
Hello <?php echo $user; ?>, your email is <?php echo $email; ?> and your are <?php echo $age; ?> years old.
Output:
Hello Tom, your email is email@host.com and your are [Unknown] years old.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment