About

You’re visiting the personal website of Joost Schuttelaar.

Take a look

Contact

You can e-mail me at jelephants @ jstsch.com (anti-spam: please remove the mammal with the big snout).

Twitter feed @jstsch

Sorry, feed is gone. API has been killed by Twitter.

Recent posts

PHP snippet: Deprecate a function

Friday July 12 2013

Sometimes you want to remove an outdated function, but can’t because users are relying on it. Then you’ll have to deprecate it instead. The snippet below logs a useful warning, which shows you where the deprecated function is used (without bugging the user):


/**
 * Function to log a deprecated warning in the error log.
 * 
 * @param string $hint Also log a hint how to fix this deprecation error.
 * 
 * @return void
 */
function log_deprecated_warning($hint = '') {
	$trace = debug_backtrace();
	$caller = (isset($trace[1])) ? $trace[1] : reset($trace);
	$hint = (empty($hint)) ? '' : '. '.$hint;
	error_log('Warning: Call to deprecated function '.$caller['function'].'() in '.$caller['file'].' on line '.$caller['line'].$hint);
}

Usage example:


function some_old_function() {
	log_deprecated_warning('We've replaced this with the new shiny banana method.');
}

Comments


(never shown, not required)
(not required)