How to deprecate a user function in PHP?

31 Jan 2010
Posted by doq

At the team with which I work we have lots of own old deprecated PHP functions that are used through out the project. We can rewrite old code to use new API but it requires a lot of time and effort and it doesn't fit in our current priorities. The best we can do now is to mark such functions as deprecated to prevent developers to use them in new code.

PHP 5.3.0 introduced two new error levels: E_DEPRECATED and E_USER_DEPRECATED. The E_DEPRECATED error level is used to indicate that a function or feature has been deprecated. The E_USER_DEPRECATED level is intended for indicating deprecated features in user code, similarly to the E_USER_ERROR and E_USER_WARNING levels.

So, generally speaking adding the code like this to the deprecated function definition will do the job:

trigger_error('Deprecated function user!', E_USER_DEPRECATED);

I have introduced the wrapper function that will show warning similar when deprecated core PHP functions like ereg are used:

  1. /**
  2.  * Special function to mark functions as deprecated. Put the call
  3.  * to it onto the top of function declaration you want to deprecate.
  4.  */
  5. function deprecated() {
  6. $stack = debug_backtrace();
  7. assert($stack[0]['function'] == __FUNCTION__);
  8.  
  9. // Get name of function that called deprecated() function.
  10. $function = $stack[1]['function'];
  11.  
  12. trigger_error('Function '. check_plain($function) .'() is deprecated',
  13. E_USER_DEPRECATED);
  14. }

You may just add the call to deprecated() function at the top of your deprecated function. Like this:

/**
 * Perform database query.
 * @deprecated
 */
function doques_db_query() {
  deprecated();
 
  $args = func_get_args();
  return call_user_func_array('mysqli_query', $args);
}

Tags:  | 

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

 
 
 
If you have found mistakes in the text then please select it and press Ctrl-Enter to send report to the site administrator.