Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override default php function

I have script wherein basename() is used 100-1000s of time, I was just thinking if we can override the function rather than changing the function name to something else in all scripts.

The problem with basename() is that it doesnt works well with names of files in foreign languages. I found one on php site http://php.net/manual/en/function.override-function.php but it needs PECL any other alternative?

like image 982
Shishant Avatar asked Oct 13 '10 21:10

Shishant


2 Answers

You can use namespaces to override existing function names:

namespace blarg;
function basename() {
  return 'whatever';
}
$base = basename();

I.e., any call to basename() within the blarg namespace will use your new version of the function.

like image 172
Alex Howansky Avatar answered Sep 19 '22 14:09

Alex Howansky


An alternative would be runkit. But that's as unlikely to be enabled on most servers.

like image 20
mario Avatar answered Sep 19 '22 14:09

mario