I already searched for the latest coding style/standards but most of the links are an year or two old and referring to this link Zend Coding Style. However the link is for version 1.12
. My only point is this, now a days most of the people are declaring the functions in this style --
public function myfun ( ) {
....
....
}
and on Zend
tutorial its something like this -
public function myfun()
{
// all contents of function
// must be indented four spaces
}
I am aware that this wont affect the functionality and its depend person to person, its just about following the best/latest
practices, let me know if thr is any relevant link regarding this for following the best practices in coding style in php
.
There is a standard developing by an initiative called the PHP Framework Interoperability Group (php-fig). This has members from some very prominent projects who are supporting the standard, some of these include:-
PRS-2 has already been agreed on and covers basic coding style. It states for function declarations and class declarations:-
Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.
Well php is now-a-days used in various environments, like Zend, Drupal, Sugar and might be many more. Cant say if all follow the same coding standards, but i can suggest you to look for doxygen coding standards.
These two links might help you.
I used to use the former style, but found the latter more readable. Especially in OOP style php where you can use the former style for classes...
class My_Class {
code here...
}
and the latter for methods...
function my_function()
{
code here...
}
In this pursuit of consistency I have found the Codeigniter style guide helpful.
Coding standards exists in almost as many variations as there exist people who care about coding standards. For some the broader scope is the important thing and "Clean Code" with correctly named functions etc is the main focus. For others it's how, when and where you place a curly bracket.
The same is true for any language (except where the layout etc of code is build into the language itself) and I would say that the most important thing is that everyone on any given team just agrees on the common need for their specific code base.
Don't try to find the optimal code style and apply it to everything. You will without question run into another developer at one time or another who has done exactly the same, but who fell in love with another style than you. If one is not flexible then and understand the root cause and usage of a code standard the resulting "discussions" could very well end up in curly-braces-position-placing-pestering discussions where everyone is out to defend their own point of view.
I have used a large variety of styles depending on the company and team I have worked in. Some has differed greatly but they have all shared the common goal, to facilitate a uniform style of the code and make it easier to understand code in places of the application you haven't looked before and to help identify issues within the code.
When deciding around a code standard in a group. Try to think
"What is the minimal set of rules we need to apply to make our code readable and understandable to us all without being a hurdle in our daily work?"
PHP does not have a style guide like Python does. Certain PHP projects have their own style guides and you follow those, if you are developing on that framework/CMS. For eg. if you were to write code for Drupal you would follow the Drupal coding standards. In certain cases your company has a coding standard. If none of these apply, follow a standard you like. Here are some you might like :
Update : rgvcorley's answer talks about PHP-FIG. So I guess there's the PHP equivalent of PEP-8 now.