The Little Guy Posted October 14, 2011 Share Posted October 14, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/249143-class-format-standards/ Share on other sites More sharing options...
jcbones Posted October 14, 2011 Share Posted October 14, 2011 I normally use camelCase, even in my variables. I have seen many people use underscores though. It is really down to personalPreference, IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/249143-class-format-standards/#findComment-1279437 Share on other sites More sharing options...
requinix Posted October 14, 2011 Share Posted October 14, 2011 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) { Quote Link to comment https://forums.phpfreaks.com/topic/249143-class-format-standards/#findComment-1279487 Share on other sites More sharing options...
KevinM1 Posted October 14, 2011 Share Posted October 14, 2011 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{} Quote Link to comment https://forums.phpfreaks.com/topic/249143-class-format-standards/#findComment-1279488 Share on other sites More sharing options...
trq Posted October 15, 2011 Share Posted October 15, 2011 I use the Zend Framework Coding Standard for PHP which is great because we use the same standard at work and you can use codesniffer pre-commit hooks within your version control system to make sure nothing is committed that doesn't follow the standards rules. Quote Link to comment https://forums.phpfreaks.com/topic/249143-class-format-standards/#findComment-1279538 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.