Jump to content

Class Format Standards


The Little Guy

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{}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.