Jump to content

Recommended naming convention


NotionCommotion

Recommended Posts

I am currently inconsistent on my naming convention, and would like to better standardize.  If starting from scratch, what would you recommend for the following for PHP?  I included what I think are the best optons below, but if it should be something totally different, please advise.  While outside of the scope of this forum, recommendations for JavaScript would also be appreciated.  Thank you

  • Classes: PascalCase
  • Name Spaces: PascalCase or camelCase?
  • Methods: camelCase or lower_case?
  • Properties: camelCase or lower_case?
  • Functions: camelCase or lower_case?
  • Variables: camelCase or lower_case?
  • Arrays: camelCase or lower_case?
  • Array Elements: camelCase or lower_case?
  • Anything I am missing?

Also, for each, should they be plural or singular?

Link to comment
Share on other sites

PSR-1 Basic Coding Standard

 

  • Properties: camelCase or lower_case?
  • Variables: camelCase or lower_case?

 

There are no explicit rules, but when you already use camelCase for methods and functions (as the standard recommends), it makes sense to use the same for variables and properties.

 

 

 

  • Arrays: camelCase or lower_case?
  • Array Elements: camelCase or lower_case?

 

What?

 

 

 

Also, for each, should they be plural or singular?

 

That definitely depends on the concrete case.

Link to comment
Share on other sites

Thank you Ignace and Jacques1.  PSR-1 is a good reference.

 


  • Arrays: camelCase or lower_case?
  • Array Elements: camelCase or lower_case?

 

What?

 

My bad, an array is just a variable.  Regarding array elements: $myArr['myElement'], what should "myElement" be?

 

Also, if my class name is MyClass, should the filename be MyClass.php or myclass.php?

Link to comment
Share on other sites

Regarding array elements: $myArr['myElement'], what should "myElement" be?

 

I'm not aware of any rules for array keys. I tend to use underscores.

 

 

 

Also, if my class name is MyClass, should the filename be MyClass.php or myclass.php?

 

PSR-4 (the filename and class name must match)

Edited by Jacques1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.