Last active
October 1, 2015 11:57
-
-
Save xeoncross/1987336 to your computer and use it in GitHub Desktop.
View Error Handling For Missing Values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 protected]'); | |
$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 protected] and your are [Unknown] years old. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment