PHPIDX Open Blog

Informative blogs and opinions.

Naming conventions in PHP

I recently came across a dialog at work where two experienced PHP developers analyze class and function naming in PHP 4.3.x. The dialog begins with the question, “Is it acceptable to name a class Null in PHP”? You may ask - why would I care? Or, why is this an issue? It’s not really, in fact the best way to avoid problems with naming is to avoid keywords as class for function names. However, if you really need that flexibility, this dialog should be of use.

So what is empty() ? :) it is not a function? In a class, it becomes a class method, not a language function. If you typed, just empty, it could also be a defined string. As, I said, it is the language parsing. :) ahh.. php :)

 


From: Griggs Domler

They aren’t quite functions, you can name a class the same as a function (built-ins like “fopen”, some user function) without any issue. The problem is simply the parsing rules.

Anything on this list:
http://us.php.net/tokens

Shouldn’t work as a class/function name (well, the ones present in version 4 anyway.)


From: Gokce Toykuyu

Empty, include (even though it is a construct) are functions. The language expects a parenthesis following those keywords. You could have a class called “false”, and it should work. As you said, it is just related to the language parsing, and well, language interpreter itself. :) ahh, PHP. :)


From: Griggs Domler

Interesting, it appears that null, false and such aren’t parsed as separate tokens (as I was thinking). But try making a class named “empty”, “include”, etc…

Also, the include_path argument to fopen has been available since at least PHP 4.3.0 (I can’t find exactly when it was added.) Making it a viable alternative to the userland function that iterates the include path to check for a files existence. (probably faster)


From: Gokce Toykuyu
As I thought, PHP allows this. Well, PHP 4.
<?php
class null {
function hello() {
echo “hello\n”;
}
}
$myNull = new null();
$myNull->hello();
?>

Your Comment