symfony-4-a

Symfony 4 and Php 7


by Teodora Ionetecu, Marius Crișan

Introduction

On the Symfony Conference we've attended this November there were a lot of talks about the soon to be released Symfony4 with its main component Symfony Flex and about what's new and what differs from previous versions. The first speaker was Fabien Potencier, the creator of Symfony framework, who made a live demo showing what Symfony 4 offers in terms of innovation and how to deal with the new system. Another main focus was on the new version of php, php 7, and there were also a lot of speakers who advised the public to update to this version for its important improvements, the most important being the performance.



PHP 7

PHP 7 was a common subject in the conference, but one of the presentations was mainly focused on presenting the new features in php7. The speaker had shown a comparison between php versions, and the result was that PHP 7 is at least twice as fast as PHP 5 (based on request per second) and PHP 7.2 is 10% faster than PHP 7.1 - so it definitely pays off to upgrade.

In addition, PHP 7 brings several new features and some of the coolest are:
-introduction of strict scalar types declaration (int, float, string and bool). For example, in the call print(sum (2, '3', 4.1)) the 3 is no longer accepted and produces an error, since it is actually a string. Before PHP 7, the type hints were limited to 2 types: objects or arrays, so with this new feature it's hoped that more correct and self-documenting PHP programs can be written.
-the Spaceship Operator <=> , is a nice addition to the language, complementing the greater-than and less-than operators. Essentially what it does is check the each operator individually, the following image makes it pretty clear.


-the Null Coalesce Operator ??, is the new short syntax for "if-set-or". It will return the left operand if it is not NULL, otherwise it will return the right. The important thing is that it will not raise a notice if the left operand is a non-existent variable.

$name = $firstName ??  "Guest";
Symfony

Symfony is a php framework build from symfony components and used to create websites and web applications. The components are being used by other big applications like Drupal, phpBB, and eZ Publish. The symfony community has over 3000 contributors, over 300.000 developers and 48 millions downloads each month.

The framework will help developers with:
-Having more structured code
-Maintaining and upgrading the code
-Faster delivery by re-using generic modules
Symfony 4


This is the latest version of the framework and it will be released at the end of November. In this version they tried to make the framework use less symfony concepts and more standard practices. The framework will be easier to understand, install and deploy and also perform better.
The biggest changes for Symfony 4 are Symfony Flex and dependency injection.
Symfony Flex


-Will be the new way to install and manage symfony applications; this is just a tool which which will improve the symfony installer.
-Will automate the tasks you have to do on a daily basis on your application, like installing and removing bundles.
Allows the application to perform tasks before and after the execution of composer commands.

When we try to install a package with Symfony flex, it will first reach the Symfony Flex server, before the installation starts. If no information is found flex won’t do anything and the installation will continue with the normal flow. If special information is found about the package Flex will return a file called “recipe”, based on which the application will decide what packages to install.
When starting a new application with Symfony Flex, it will install only the minimal required packages instead of having everything from the start. When you will need more, you can just install them along the way.

Dependency injection 


Dependency injection means that instead of creating a dependency within the class, or via a factory you need to pass the dependency externally. One of the biggest advantages of dependency injection is that it can make testing a lot easier, since the code you need to test does not depend on other dependencies and you can mock everything you need.

In Symfony we have a dependency injection component, which allows you to centralize the way objects are constructed within your application. In order to create a service we have to add the class and parameters which are needed by the constructor in a services configuration file.
Dependency injection in Symfony 4

Starting from symfony 3.3 new features were introduced, which will help making the development faster, without sacrificing predictability.

The biggest changes are:
-Services are now loaded automatically, you no longer need to define services one by one, but define all classes from a folder as a service.
-Auto-wiring made by default; use type-hint instead of service id. Now the services id will be less important and types (class or interface) are more important
-Controllers are registered as services. With this change you can now auto-wire arguments to your controller action methods, in the same way you do it with the constructor of services
-Auto-tagging with auto-configure. This can be done by setting the auto-configure key to true, under _defaults
-Auto-configure with __instance of. This will act as a default definition template, but only for services whose class matches a defined one.

Advantages

Credits:

Leave a Reply

Your email address will not be published. Required fields are marked *