开发者

PHP: Ambiguous function names

开发者 https://www.devze.com 2022-12-20 19:31 出处:网络
I am 开发者_StackOverflow中文版new to php and i have seen rather Ambiguous function name convention in php and this confuses me as i regularly forget function names. I suppose i should give you an exa

I am 开发者_StackOverflow中文版new to php and i have seen rather Ambiguous function name convention in php and this confuses me as i regularly forget function names. I suppose i should give you an example.

If function for encoding is htmlentities then why it's opposite is named html_entitiy_decode rather than something like entitiesdecode or the one which is more close to htmlentities.

There are matching function names too but i think php does not have a consistent approach to naming its functions.

Any ideas please as it makes hard for me to remember function names. Thanks


No, PHP doesn't really have a consistent approach to functions names -- that's mainly for two reasons :

  • Historical reasons : once functions have been released with those names, they can't be changed
  • Extensions / libraries : PHP is a "glue" language that incorporates several external libraries, and it often used the names of the functions of those libraries -- which are not necessarily consistent.

Fortunately, the PHP community and developpers are aware of that, and try not to repeat this mistake now, when they develop new functions -- but things will most likely remain like this for old functions.


The best you can do is to adopt one naming convention for your classes/functions/methods/variables, and respect it in your projects ; that'll already be a good start.


There is somewhat related question and answer in the PHP FAQ about parameter order:

I cannot remember the parameter order of PHP functions, are they random?

PHP is a glue that brings together hundreds of external libraries, so sometimes this gets messy. However, a simple rule of thumb is as follows: Array function parameters are ordered as "needle, haystack" whereas String functions are the opposite, so "haystack, needle".

The first sentence of the answer can be applied to function naming as well.

For your own functions, have a look at the Userland Naming Guide and/or consider following one of the Coding Conventions like Zend or PEAR.


I'm afraid that this is a problem with PHP that you're going to have to live with. You could wrap these "misnamed" functions with your own, but that will obviously make calling those functions more expensive.


I would give simple answer, the fact is that it is because of compatibility. A function once given a name can not be reversed as it has been used in thousands of projects and the language core as well as pear and pecl. Thanks

0

精彩评论

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

关注公众号