This is what modern PHP looks like

Create another one.

That was a big problem, which I discussed in another article, and made me start giving tests a chance.

The first tool I was presented was PHPUnit.

As stated in the official sitePHPUnit is a programmer-oriented testing framework for PHP.

It is an instance of the xUnit architecture for unit testing frameworks.

So, PHPUnit is a framework for helping you create tests for your projects, unitary tests.

It gives you several functions to test the outcome of your code and generate a nice output with the result from those tests.

Since I started thinking about tests, reading and talking to people about it, I’ve discovered another great tool, which complements the work you’ve put in those unitary tests, it is calle Behat, which is a BDD framework for PHP.

BDD (Behavior-Driven Development) is a development process which came from TDD (Test-Driven Development).

Those acronyms are not important now, what is important is that you can specify your tests using a more natural language, a language that non-technical folks can understand.

This language is called Gherkin and is used to describe the expected behavior being tested.

A test description, using Gherkin, looks like thisBehind those lines there is PHP code that is called whenever there is a match between a line and a regex pattern specified in the PhpDoc of the method.

This code implements those steps and what a real user would do, using your SDK, application or web system.

The workflow with Behat is so smooth.

After everything properly configured, you start writing all possible scenarios for testing a feature.

The first time you run Behat, it gives you all the method templates you should add to your PHP Context class in order to implement each step in a scenario.

After that, you start writing the actual code for each step and keep repeating this cycle.

Implement PHP code for a stepRun testsIf everything is fine, write PHP code for another stepIf something is broken, fix itAfter half an hour of configuring and reading documentation, you are prepared to use Behat, you’ll see that in the end it is all PHP code and you already know how to program with it.

Continuous IntegrationContinuous integration (CI) is a process – a way to do something, and this thing, for us software engineers, is creating software.

In plain English, it is the act of incorporating small chunks of code constantly (maybe several times a day) into your code base.

Code which has been tested and did not break anything.

CI helps you automate the building, testing and deployment of your applications.

With a few clicks you can integrate your GitHub project with Travis CI and every push to your repository will run those tests you created with PHPUnit and Behat, telling you whether the the last feature you’ve implemented is ready, or not, to be merged.

Besides that, you can use Travis CI to deploy your code to production and staging.

Having a nice pipeline of work with a well defined process is great and Travis CI can help you with this job.

Follow this nice Getting started and discover how interesting it is to think about the process of software development, not just the code itself.

Adhere to PSR-1 and PSR-2If you don’t know what PSR is, you should.

Actually, PSR stands for PHP Standard Recommendations and is proposed by PHP-FIG (PHP Framework Interop Group), a consortium formed by members from the biggest PHP projects, frameworks and CMSs, which are thinking about the future of the language, ecosystem and discussing standards to be followed.

For a long time, PHP had no coding style.

I’m not that old, but every time I looked into someone’s project or library, it was following a different style.

Sometimes the bracket was left in one position, sometimes it was put in the next line, different approaches were used to deal with long lines and every other combination of style and preference you could think of.

It was a mess.

PHP-FIG does many other jobs, but by proposing a single unity of code, they are saying “Let’s stop worrying about code style, let’s everyone follow a standard and start thinking about creating great software”.

Now, whenever you take a look at someone’s code you just worry about understanding how it works, not blaming the format, the structure.

There are, until the end of this article, 9 accepted PSRs proposing common solutions for common problems.

But if you don’t know anything about those standards, start with the PSR-1 and PSR-2.

These standards propose the modern PHP coding style.

Make sure you read them before start using them.

Don’t think you’ll remember all of them when coding, it is a process, but to make you sure, there are tools to help you with it.

PHP CodeSniffer is a tool you can find on Packagist that you can install with Composer.

I don’t think the repository name was the best choice, because it ships two different tools, phpcs and phpcbf.

Phpcs is the code sniffer, it will scan your entire code, looking for parts that are not following the configured coding standard.

You can use several coding standards with phpcs and you can even create your own.

At the end of the code scan, phpcs shows you a list of the pieces of code not following the standard.

It is great.

Now, how to change everything which is wrong?.You could open every file, change the code, run phpcs again, see the error not being shown, and repeat the process.

It’ll be extra boring.

To solve this problem, PHP CodeSniffer came with another tool, called phpcbf, or PHP Code Beautifier.

You run phpcbf, following the same rule set and, voilá, it fixes everything for you, or it tries to do its best without breaking your code.

Try to create the habit of running phpcs and phpcbf before pushing any changes in your code to the repository, this will ensure that all of your code adhere to the standards and if someone likes your tool/project and wants to contribute, they will have no problem reading it.

FrameworksI’m not going to dedicate too much time discussing frameworks.

There are several good ones out there, each one with its ups and downs.

Personally, I prefer not to use those big frameworks, with everything inside.

I like the idea that you must use just what you need.

If you need a HTTP client, use Guzzle.

If you need a template engine, use Twig.

If you need a router, find a good component which suits your needs and use it.

Glue these components together and create your application.

Symfony is doing a great job towards this concept.

You can use the entire framework for a project, or you can just take whatever you want and use it.

Simple as that.

However, whenever I need a framework to write an application, I chose one of the so called microframeworks.

They are really small, offer just the basics and are easy to customize and easier to make them follow your project structure.

My microframework of choice is Slimframework and I think you should read about it.

It is simple for doing small projects, but it gets a bit more complex for bigger ones.

By the way, and this is for those who are starting with programming, I really think that before adopting a framework and dying for it, you should try to create your own.

This will give you the understanding of the whole mechanism and ease the adoption of a big one.

The Modern PHP ToolsetLet’s finish this article with a list of links.

To me, these components, tools and libraries represent a great deal of what Modern PHP is:Slimframework: a nice and cool microframeworkSymfony: a bigger framework which is filled with great and reusable componentsGuzzle: a simple and easy to use HTTP clientPHPUnit: a framework for unitary testingBehat: a framework for Behavior-Driven DevelopmentPHPCS/CBF: code sniffer and code beautifierFaker: fake data generatorPsysh: a runtime developer console (CLI) full of amazing featuresComposer: dependency management and other useful featuresPackagist: package repositoryTwig: template engineThe title was really pretentious, I know.

What I really wanted to show here is that PHP is evolving and the ecosystem is evolving at the same (maybe faster) pace.

.

. More details

Leave a Reply