Sunday, February 22, 2015

to alias a function with a different name in PHP

Solution 1:

function AliasFunctionName($optArr = array())  {
  OriginalFunctionName($optArr);
}

Solution 2 (PHP 5.3+):

If you are worried about having to change AliasFunctionName() should you change the number of parameters for OriginalFunctionName() then you might want to do the following instead:

function AliasFunctionName() { 
  return call_user_func_array('OriginalFunctionName', func_get_args());
}

Solution 3 (PHP 5.6+):

use function OriginalFunctionName as AliasFunctionName;

Reference:

http://stackoverflow.com/questions/1688711/can-we-alias-a-function-in-php

No comments: