Best Practices for Modern PHP Development

PHP is a complex language that has suffered years of twists, bends, stretches, and hacks. It’s highly inconsistent and sometimes buggy. Each version has its own unique features, warts, and quirks, and it’s hard to keep track of what version has what problems. It’s easy to see why it gets as much hate as it does sometimes.

Despite that, it’s the most popular language on the web today. Because of its long history, you’ll find lots of tutorials on how to do basic things like password hashing and database access. The problem is that out of five tutorials, you have a good chance of finding five totally different ways of doing something. Which way is the “right” way? Do any of the other ways have subtle bugs or gotchas? It’s really hard to find out, and you’ll be bouncing around the internet trying to pin down the right answer.

That’s also one of the reasons why new PHP programmers are so frequently blamed for ugly, outdated, or insecure code. They can’t help it if the first Google result was a four-year-old article teaching a five-year-old method!

This document tries to address that. It’s an attempt to compile a set of basic instructions for what can be considered best practices for common and confusing issues and tasks in PHP. If a low-level task has multiple and confusing approaches in PHP, it belongs here. (more…)

Continue Reading Best Practices for Modern PHP Development

PHP – Strict Types

Being able to declare types, such as intstring, or bool was a massive improvement added to PHP with the release of 7.0. However, there is a small but important factor that you might not be aware of.

The Problem

The problem is best explained with code. Below is a function that just adds two integers together: (more…)

Continue Reading PHP – Strict Types

PHP Frameworks: Choosing Between Symfony and Laravel

Today, when starting a new project, one of the key decisions is to pick the right framework. It’s become hard to imagine building a complex web application from scratch nowadays without one.

Many popular languages for web development have their “default” framework, such as Ruby on Rails for Ruby, or Django for Python. However, PHP has no such single default and has multiple popular options to choose from.

According to Google trends and GitHub, the most popular PHP frameworks are Symfony with 13.7k stars and Laravel with 29k stars (at the time of writing this article).

In this article, I am going to compare these two frameworks and show you how to implement simple, everyday features with each. This way, you can compare the code of real-life examples side by side.

This article presumes strong PHP skills and an understanding of the MVC architectural paradigm, but no previous experience with Symfony or Laravel is required. (more…)

Continue Reading PHP Frameworks: Choosing Between Symfony and Laravel

Восемь причин изучить PHPDoc

Часто встречаю вопрос о том, что же это за странные блоки комментариев постоянно генерируются в представлениях:

<?php
<?php
/* @var $this yii\web\View */
/* @var $searchModel \app\models\search\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
?>

в ActiveRecord-классах: (more…)

Continue Reading Восемь причин изучить PHPDoc

Модульное тестирование поведения Yii2 с помощью Codeception

В разработке программного обеспечения написание автоматических тестов часто отодвигается на второй план более насущными проблемами. Так и в моем случае, код писать приходилось, а тесты к нему — нет. При этом давно хотелось попробовать модульное тестирование собственного кода, а тут под руку подвернулось поведение Yii2 ManyToMany Behavior, о котором уже писали на Хабре. Это поведение я сначала немного расширил, а затем решил собрать комплект тестов.

Сами тесты, в том числе те, о которых идет речь в этой статье, можно посмотреть в репозитории по ссылке выше. Все команды выполнялись под Windows с глобально установленным composer, но я думаю, что разработчики, пользующиеся Linux, без затруднений смогут адаптировать их под себя.

Далее мы рассмотрим настройку Codeception с модулем для Yii2 и создание тестов для поведения.
(more…)

Continue Reading Модульное тестирование поведения Yii2 с помощью Codeception