I know a lot of people have their own comforts and styles when it comes to scripting and coding, but...

Some language documentations specifically state a naming convention that is considered best practice for their language, like Java specifying camelCase.

However, PHP's online documentation doesn't specify a naming convention (from what I've found, at least). Google searching this topic yields a lot of discussions akin to "What is generally accepted?" but I was wondering if people here know if there is an authoritative answer on the topic.

So, is it best practice to declare my variables...

$likeThis

or

$like_this

or even

$LikeThis

What about functions?

Anyone?

Member Avatar for Zagga

Hi TySkby,

I was reading about this recently too and like yourself, couldn't find a 'standard' used in PHP. I guess PHP developers are a little more relaxed than some of their counterparts (dons flame proof jacket).
It seems that it doesn't matter what naming convention you use, as long as you are consistent.

Member Avatar for diafol

I've been to underscores and camelCase for vars and funcs. As mentioned, as long as you're consistent, I don't think it matters. Perhaps afficionados will bray at that. I look at underlying source code from the top open source providers and can't find a single format for all of them - some use camel some use underscore and some mix them up for functions and variables. I've given up trying to 'conform' as I don't know to which format I should conform. However, one thing I do try to do is make the names meaningful or have prefixes that tell me if I'm dealing with raw data, sanitized data, passed parameters. Sometimes, I'll prefix with single letters for data types. Depends on the app and the complexity.

Hello, I can’t recall an authoritative Java tutorial that “specifying camelCase”. One of pylons of Java is architectural neutrality and in PHP this is more generic as it is programming neutrality.

What I mean is that you can write PHP also if it where JSP and a complex JAVA structure. So I believe that there is not such thing as an authoritative answer to that. I can share with you what I use, that was the conclusion before some years of research on defirent frameworks (including Zend) and mixed with common logic. For attributes I use camelCase and for setters and getters functions (methods) getCamelCase setCamelCase that is the case also for all methods.

Also I believe that there is no need to have deferent conventions for static variables (like in capitals) because you can’t access them in PHP without the distinguee self:: or parent:: . Class names in my view fallow the autoload conventions. So if you are in the directory src the Controller_Welcome class should be in the directory anything/Controller/Welcome.php .

As I mentioned I am not proposing anything without knowing the usage of PHP. I am just sharing my views with you …

I've never seen an official naming convention either. jkon would have described the closest to what I'm familiar with.

If you are going to code to a standard I would suggest looking at the PEAR standards and than looking at their implementation in some larger projects.

PEAR: http://pear.php.net/manual/en/standards.php
Zend Framework: http://framework.zend.com/manual/en/coding-standard.coding-style.html
Symfony: http://trac.symfony-project.org/wiki/HowToContributeToSymfony#CodingStandards
Doctrine: http://www.doctrine-project.org/documentation/manual/1_0/nl/coding-standards

From what I've seen those big projects use variation of the PEAR standards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.