laurent
laurent

Reputation: 90863

Is it possible to override a default PHP function?

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?

Upvotes: 4

Views: 1151

Answers (4)

Fabio
Fabio

Reputation: 19216

You could try the runkit extension but it's considered a bad practice in production environments. See also Redefining PHP function?

Upvotes: 1

martinstoeckli
martinstoeckli

Reputation: 24151

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.

Upvotes: 1

Spudley
Spudley

Reputation: 168853

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.

Upvotes: 1

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14951

No, but with PHP version >= 5.3.0 you could use namespacing though.

Upvotes: 3

Related Questions