Many times I find code difficult to read, since it is poorly structured or without indentation. In SOes we can find multiple examples of this.
Searching the site for a question that addresses this issue, I came across this one from @A.Cedano , PHP Naming Convention , but it doesn't really address style or best practices. I also did not find any questions that address PSR 1 and 2.
If I could find another similar one referencing html, css and js. Official style guides for HTML, CSS and Javascript
That's why I ask this question:
What are the tips or recommendations regarding the style and order of the code that should be taken into account when developing with PHP?
The best style guide that we can use when developing a project in PHP is the PHP Standards Recommendations (PSR) . Specifically PSR 1 and 2 that refer to basic coding and coding style respectively.
PSR-1 Basic coding standard
<?php ?>
and/or<?= ?>
tag, no other opening/closing tag (example<? ?>
,<% %>
, etc...).StudlyCaps
.MI_CONSTANTE
) separators.camelCase
.PSR-2 Coding style guide
null
,true
andfalse
must be lowercase.namespace
.use
.extends
The e keywordsimplements
must be declared on the same line as the class name.{
in classes and methods must go on the next line, and closing braces must go on}
the next line after the body.public
,protected
orprivate
) must always be declared on all properties and methods.( )
There must be no blank spaces between the names of functions and methods and the parentheses (example:miFuncion()
).{
for control structures (example:if
) must follow on the same line, and the closing brace}
must proceed to the next line after the body.Examples:
Namespace and use
Extends and Implements
Properties
Methods
Methods with multiline argument
abstract, final and static
Calls to methods and functions
if, else if and else
switch case
while and dowhile
for
foreach
try and catch
Source: https://github.com/php-fig/fig-standards
Complementing @Xerif's response, it is appropriate to mention that these PSRs are today proposed, maintained and implemented by the PHP Framework Interoperability Group , which has weight because it brings together representatives of the most important frameworks and libraries in the PHP ecosystem:
(The only big one that is not is Laravel, but Taylor Otwell left the FIG because he didn't have time to participate in the debates, vote or propose. However, part of Laravel adopts some PSRs)
Basically, although this group does not decide the features that are in the core of the PHP engine, they can decide how to operate with them by adopting conventions that allow interoperability between frameworks.
For example, PHP does not have a native class that handles dependency injection. A construct like that everyone implements as they want. However, thanks to the existence of PHP-FIG, and an agreement reached among its members, they came out with PSR-11, which specifies how a dependency wrapper should behave. Today if you install a framework that uses it
symfony/dependency-injection
as an injector, you are free to change it tophp-di/php-di
because they implement the same interface.The same goes for handling of
$request
and$response
on routes. Today most frameworks adhere to PSR-7. That means for example that if you want to change the engine of your application fromSilex
aSlim
you can leave your routes as they are.Although this does not have to do specifically with the style, these PSRs are vital for the PHP ecosystem and reinforce the cohesion between the different actors.