开发者

Is it possible to override a default PHP function?

开发者 https://www.devze.com 2023-03-29 20:53 出处:网络
I would like to override the _() PHP function so that it supports one or more arguments. Is it possible to 开发者_如何学JAVAdo that using PHP code?No, but with PHP version >= 5.3.0 you could use names

I would like to override the _() PHP function so that it supports one or more arguments. Is it possible to 开发者_如何学JAVAdo that using PHP code?


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号