Jump to content

Recommended Posts

What are some common standards to writing a php class?

 

One thing I think is a standard is using "Camel Case" (example):

public function myFunctionName

instead of using:

public function my_function_name

 

Is that true, and are there any others that you know about? I would like to hear what they are!

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/249143-class-format-standards/
Share on other sites

I tend to do camel case for normal terms that contain multiple words, then underscores for some kind of relation - often (but not always) to another variable, and generally protected or private.

public  $dirSeparator = null;
private $dirSeparator_windows = "\\";
private $dirSeparator_unix = "/";

public function parseXmlFeed() {

private function parseXmlFeed_channel($channel) {

private function parseXmlFeed_item($item) {

I use camel case as well.  I'm not a fan of underscores in general.  Looks ugly, IMO, and requires extra key presses.  The exception is as a prefix to private members.  I'm torn on using them, so I often simply leave the first letter of my private members lowercase:

 

private function somePrivateFunction(){}

 

I capitalize public members, like so:

 

public function SomePublicFunction(){}

 

Class names themselves always have capitalized first letters.  Interfaces have the 'I' prefix:

 

class Example implements ISomeInterface{}

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.