Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created August 25, 2012 02:14
Show Gist options
  • Save felipelavinz/3458853 to your computer and use it in GitHub Desktop.
Save felipelavinz/3458853 to your computer and use it in GitHub Desktop.
Deprecated functions exception handling
<?php
// versión re-escrita
function array_to_object($array = array()) {
try {
throw new flv_exception_deprecated( __FUNCTION__ );
} catch ( flv_exception_deprecated $e ) {
flv_handle_exceptions( $e );
return (object)$array;
}
}
// versión anterior del código :-P
/*function array_to_object($array = array()) {
if (!empty($array)) {
$data = false;
foreach ($array as $akey => $aval) {
$data->{$akey} = $aval;
}
return $data;
}
return false;
}*/
<?php
class flv_exception_deprecated extends Exception{
protected $replacement;
function __construct( $function, $replacement = '' ){
$this->message = trim($function);
if ( $replacement ) $this->replacement = $replacement;
parent::__construct();
}
function getReplacement(){
return $this->replacement;
}
function __toString(){
$out = 'DEPRECATED function: '. $this->getMessage() .' on '. $this->getFile() .':'. $this->getLine();
$replacement = $this->getReplacement();
return $replacement ? $out . '. Please use '. $this->getReplacement() .' instead.' : $out;
}
}
<?php
function flv_handle_exceptions( $e ){
if ( WP_DEBUG ) {
error_log( $e ."\n". $e->getTraceAsString() );
}
return;
}
set_exception_handler('flv_handle_exceptions');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment