php[architect] November 2018

(singke) #1
http://www.phparch.com \ November 2018 \ 21

Getting Started With Php? Let’s Start the Right Way!


  • Added the Zend OPcache^6 extension for opcode caching,
    improving performance;

  • And many more changes.
    These releases kept PHP 5 polished and useful but the big
    move was coming: PHP 7!


PHP 7: Everything is New Again


Yes, PHP 7! There’s no PHP 6. Would you like to know why?
Read the PHP6 RFC^7.


Well, PHP 7 came aboard. Behind PHP there’s an engine
called the Zend Engine; it’s responsible for parsing the PHP
code. It was rewritten to its version 3, in other words, a brand
new language, with a few breaking changes, but also many
new features and a fantastic performance improvement. PHP
7 was twice as fast as PHP 5 and has 50 percent less memory
consumption. Pretty good, no?


PHP 7 was the version to consolidate PHP. With all those
amazing new features from the past few years, now PHP 7 is
faster and so much better to work with, allowing you to write
better code.
PHP 7 came with big and small changes. It made the
language more fun to work with and changed some syntax
to make things easier to be written. For example, the uniform
variable syntax, that allows you to something like this:


// Before
$foo = new Foo();
$foo->bar();


// After
(new Foo())->bar();


Or even this:

// Before
$foo = $foo->bar();
echo $foo[ 0 ];


// After
$foo->bar()[ 0 ];


And much more. For a complete list of all possibilities you
have with the introduction of the uniform variable syntax^8.


Another exciting addition to PHP 7 was the null coalesce
operator: ??. It’s also present in many other languages like Perl
(with //), C# (??) and Python (or). It allows you to check if
that variable (or array position) exists before using it.


// Before
$age = isset($data['age'])? $data['age'] : 100 ;


// After
$age = $data['age'] ?? 100 ;


6 Zend OPcache: http://php.net/opcache


7 PHP6 RFC: https://wiki.php.net/rfc/php6
8 uniform variable syntax:
https://phpa.me/rfc-uniform-variable-syntax


There’re also other small changes like the spaceship operator
<==>, that allows you to do a three-way comparison^9 in PHP
and also the possibility of declaring anonymous classes^10.
All those changes were good, but the following changed
how you write PHP code, making you think more about what
you’re doing to avoid mistakes and create stronger code.
As you know, PHP is not a strong typed language, but this
was improved with the introduction of return type declara-
tions and scalar type declarations. Now you can force a specific
type as a parameter in a method call, and also force the type
that method is returning (see Listing 2).
In the example above you are saying the $sum variable must
be a float and the $title must be a string, but also that you
must return a float from this method.
Of course, PHP 7 doesn’t require you to use these two new
features by default; they are optional but a good recommen-
dation. If you want to enforce them, you can enable the strict
mode on a file by file basis, adding declare(strict_types=1)
at the top of the PHP file. Otherwise, PHP 7 will cast things
to the expected types for you whenever possible.
Another small change which reduces the number of written
lines of code was enabling group use declarations^11. Do you
remember when we talked about namespaces before, using
the use keyword to import classes you want to use? Now you
can import many classes belonging to the same namespace at
once as shown in Listing 3.

9 three-way comparison: https://phpa.me/rfc-spaceship-op
10 anonymous classes: https://wiki.php.net/rfc/anonymous_classes
11 group use declarations: https://phpa.me/rfc-group-use

Listing 2


  1. // Before

  2. public function create($sum, $title)

  3. {

  4. // your code here

  5. }



  6. // After

  7. public function create(float $sum, string $title): float

  8. {

  9. // your code here

  10. }


Listing 3


  1. // Before

  2. use Package\Http\Request;

  3. use Package\Http\Response;

  4. use Package\Http\Middleware;

  5. use Package\Http\BaseController as Controller;



  6. // After

  7. use Package\Http{ Request, Response, Middleware,

  8. BaseController as Controller };

Free download pdf