I would like to override the _()
PHP function so that it supports one or more arguments. Is it possible to do that using PHP code?

- 88,262
- 77
- 290
- 428
4 Answers
No, but with PHP version >= 5.3.0 you could use namespacing though.

- 2,250
- 17
- 15

- 14,888
- 4
- 41
- 59
You could try the runkit extension but it's considered a bad practice in production environments. See also Redefining PHP function?
-
Bad practice, and dangerous - last release is 2006. – Wesley van Opdorp Aug 23 '11 at 11:17
-
@WesleyvanOpdorp I said that in my answer, but it's still an answer to the question. – Fabio Aug 23 '11 at 11:24
-
Sorry, 'Not only' should be added at the start of my comment. – Wesley van Opdorp Aug 23 '11 at 11:41
It is possible, using the Runkit extension.
However, it's generally not considered a good idea, except for use with things like unit testing, where you may want to isolate some of your functionality.
For general use, you shouldn't be overriding built-in functions because it makes your code harder to maintain, and opens you up to some very hard to debug issues.
Also, the Runkit extension is marked as 'experimental', which means it really shouldn't be used in a production system.

- 166,037
- 39
- 233
- 307
Really don't do this! Even if you are the only developer on this project and know that your project won't be successful, one can never know how long your code will be in use (often much longer than you would think). Should another developer have to dive into your code, he will have a very hard time, because he/she cannot rely on PHP itself.
A better way would be to write your own methods/functions, which then call the PHP function you want to overwrite. This way a developer immediately can see, that this is not the standard PHP function, and even if PHP will allow other parameters in future versions, you will have a clean solution.

- 23,430
- 6
- 56
- 87