6

I am 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

4
  • What I usually do before I fire up my editor is fire up the PHP docs. I've downloaded these in a comfortable .chm format for Windows (I even believe there are .chm viewers for other OS's too). Downloadable docs are here: php.net/download-docs.php Commented Feb 16, 2010 at 12:13
  • 1
    Here's a .chm viewer for OS X: chmox.sourceforge.net . And here are several for Linux: linux.com/archive/articles/122171 Commented Feb 16, 2010 at 12:15
  • @fireeyedboy: yeah that is one of the best resource to have open. Commented Feb 16, 2010 at 12:42
  • @phpBOY: +1: I had the similar problem when i started with PHP :) Commented Apr 20, 2010 at 13:43

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Pascal: great and most logical answer i belive, sorry i could not vote you up because it requires me to have reputation of atleast 15 but anyways this really helped, Thanks :)
@Pascal: +1 i am able to do so now :)
3

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.

Comments

2

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.

Comments

0

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

1 Comment

@Sarfraz: yeah that seems to be pretty correct and short answer thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.