So far, we discussed performance-related topics. Now, in this chapter, we will study best practices in PHP applications' development and deployment. This is a vast topic, but we will cover it briefly. PHP provides all levels of programmers with the ability to write quality code easily and quickly. However, when the application advances to a more complex nature, we forget to follow the best practices. To produce a high performance PHP application, it is necessary to keep in mind the performance at every line of the code.
We will cover the following topics:
There are too many coding styles out there, such as PSR-0, PSR-1, PSR-2, PSR-3, and so on. Programmers can use different standards as they want, but it is necessary to follow a standard that is already used in the libraries or a framework in use to make the code more readable. For example, Laravel uses the PSR-1 and PSR-4 coding standards, so if we are developing in Laravel, we should follow these coding standards. Some PHP frameworks, such as Yii 2 and Zend Framework 2, follow the PSR-2 coding standards. However, none of these frameworks stick to a single standard; most of them follow a mixed standard according to their requirements.
The important point is to follow the standard that is used in the libraries used in the application. An organization can also use its own coding standards for internal purposes. It is not a requirement for coding; it is a requirement for readability and producing quality code that others can understand.
PHP Framework Interop Group (PHP-FIG) is a group whose members defined coding standards for PHP. Full details about PSR standards can be found on their website at http://www.php-fig.org/.
Instead of discussing a specific coding standard, let's discuss best practices in coding styles for PHP:
class Foo
{
…
…
…
}public function phpBook($arg1, $arg2, $arg3)
{
…
…
…
}extends and implements keywords must be on the same line as the class declaration. Here's an example:namespace Packt\Videos;
use Packt\Books;
use Packt\Presentations;
class PacktClass extends VideosClass implements BaseClass
{
…
…
…
}class PacktClass
{
public $books;
private $electronicBooks;
…
…
…
}abstract keyword, it must come before the class keyword for classes, and the final keyword must come before the method's visibility in the case of methods. On the other hand, the static keyword must come after the method visibility. Take a look at this example:abstract class PacktClass
{
final public static function favoriteBooks()
{
…
…
…
}
}true and false keywords. Constants must be declared and used in capital case.if ($book == "PHP 7") {
…
…
…
} else {
…
…
…
}for ($h = 0; $h < 10; $h++) {
…
…
…
}
foreach ($books as $key => $value) {
…
…
…
}
while ($book) {
…
…
…
}For the purpose of this book, I did not follow the rule of the opening brace being on the same line as the control structure declaration and always used it on the next line of the declaration. I did not find it clearer; it is a personal choice, and anybody can follow the standards mentioned here.
Standards are good to follow as they make the code more readable and professional. However, never try to invent your own new standards; always follow those that are already invented and followed by the community.