2

I come from Java / Objective-C to PHP and find it horrible. I mean, PHP is nice, really. But when you look at a bunch of variables, you don't know: Is that an number? Is that an string? Or ist that even a fancy object that can perform actions when calling methods on it?

So I wonder if there are helpful re-usable naming conventions for variables to help figure out if something is an object, or if something is just a boring variable. I'd say if something is an object, i.e. an instance of a class, the first character must be BIG. But it's just a guess. Hope to read some tips from PHP pros :-)

3
  • Number or a String ? Well, it can be both, depending on where you are in your script (variables are not as strongly typed as in JAVA). Commented Dec 22, 2009 at 21:08
  • 5
    The real naming issue with PHP is the completely inconsistent naming schemes and parameter orders in the built-in library. Commented Dec 22, 2009 at 21:11
  • 3
    There are no enforced naming conventions in Objective-C whatsoever, so this is mostly in your head. Commented Dec 22, 2009 at 21:13

5 Answers 5

10

As in other languages, there is no single coding standard in PHP. You can, among others (see the comments), check out the Zend coding standards, they are quite highly regarded as they are very close to (and partly identical with) the PHP core development team.

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

1 Comment

I also stick to the Zend coding guidelines: camelCasedVariables , This_Class_Naming_Scheme, BIG_CONSTANTS and identifiers that actually tell you what they represent like $databaseConnection instead of $dbc.
3

What's to prevent you from using the conventions you're already familiar with? It's likely that there is nothing unique to PHP that you've not already witnessed before.

Comments

0

I usually stick to this simple rule

Meaningfull Camel Case Variable names (with type prefix before) ex : intNbDaysLeft would be good for an integer

3 Comments

Yuck! How'd you prefix a variable holding a Zend_Gdata_MimeBodyString then? :)
When the dataType is long and complex, I usually take the first letter of each word. So you'de get something like zgmbsVariable. Some people reduce it only to the first 3 letter, or what we do is prepend an o for Object + a meaningfull name. Again, I insist on MEANINGFULL. If you understand what you are writing and other people do,then it's fine. It's all a matter of how you're used to work.
Meaningful + systems hungarian? I'm pretty sure that's an oxymoron.
0

I use Zend Eclipse, so I describe my variables like this, to make sure they're intellisensed. Then, I worry less about, "what is this?"

/**
 * @var Zend_Form_Element
 */
protected $_member;

or

/**
 * @param Zend_Form_Element $input
 */
function do_it( $input )

or

$my_var = some_function(); /* @var $my_var Zend_Form_Element */

1 Comment

That is PHPDoc, not "Zend Eclipse convention."
0

You should quickly discover that there is little type hinting in PHP, and the reason for that is clear, in this dynamic language type can change, so name is not very important.

Using PHPDoc with Eclipse or NetBeans helps, but you cant rely on that, if you want to be sure that Your variable is of specific type, you have to check it (is_array(), instanceof, ect.).

Also, PHP does a lot for you, it will convert betwen types on Your behalf, it will act differently depending on the type.

PHP mostly works with strings, so most variables are of that type. Applications can have lots of types, but a single method shouldn't use too many. If you find yourself using so many objects that you cant easily keep track of their instance names, then it's some design problem, not naming convention issue.

If you want to be sure you have not used some variable wrongly, be sure to unit test. The standard in PHP is phpunit

1 Comment

"PHP mostly works with strings, so most variables are of that type" ... [Citation needed]

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.