php[architect] November 2018

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

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

PHP 7 is here to stay. It had a lot of significant improve-
ments and syntax changes, making PHP a modern language
again. It was just the beginning, we have more coming in the
next sections.

PHP 7.1 And 7.2
Now you know how PHP has changed since PHP 5.3, I will
present you a set of small changes which have come along
in the last two years to bring flexibility and security to the
language.
In December 2016, PHP 7.1 was released with more perfor-
mance improvements—two syntax changes that brought
flexibility to the language: the void return type^12 and class
constant visibility modifiers^13.
Since PHP 7.0 we have return type declaration, but some-
times you just need to return null. For example, you want to
return an integer or just null. That was not possible with PHP
7.0, but with 7.1 you can write ?int. This allows you to return
int or null. The same works for object instances, for exam-
ple, using ?DateTime—you say you must return an instance
of DateTime or just null. And there’s more; this is not only for
returning values from methods but also for methods declara-
tion as well:


public function get(string $name, ?int $age): ?User
{
// code
}

Another great improvement from PHP 7.1 was the class
constant visibility modifiers that made possible to use a
constant just for the current class scope. You can say a class
constant is public, protected, or private, respecting the same
visibility principles we have in PHP for methods and prop-
erties.
In November 2017, the PHP community released PHP 7.2,
the current stable release as of this writing. It came with four
interesting improvements: the object parameter and return
type^14 , the Libsodium extension^15 , abstract method overriding^16
and parameter type widening^17. For now, I’m gonna focus only
on the first two.
PHP is flexible enough and when talking about parameters
and return types, as it should be. Sometimes you just want to
return an instance of some class, an object. You don’t want to
force a specific instance name, just an object.
Since PHP 7.2 you can use the object type for method
declarations and also for the return type:

12 void return type: https://wiki.php.net/rfc/void_return_type
13 class constant visibility modifiers: https://phpa.me/rfc-class-const


14 the object parameter and return type:
https://wiki.php.net/rfc/object-typehint


15 Libsodium extension: https://wiki.php.net/rfc/libsodium
16 abstract method overriding: https://phpa.me/rfc-abstract-override


17 parameter type widening: https://phpa.me/rfc-parameter-widen


public function (object $foo): ?object
{
// code
}

In the example above you are stating you need an object
as a parameter and also an object (or null) as a return. That
brings you flexibility, allowing you to make your code more
generic which is also good.
In addition, PHP 7.2 came with a significant and amazing
change: it included Libsodium as a core extension. That said,
PHP became the first programming language to add modern
cryptography library in its core.
What does this mean? This means PHP is improving year
after year, following and using our most secure option for
implementing cryptography. In this case, PHP came first.

PHP 7.3 And the Future
The changes don’t stop here. In the next few months, PHP
7.3 is going to be officially released. It comes with some
improvements like flexible heredoc and nowdoc syntax,
support for references and array deconstruction with list(),
PCRE2 support, hrtime() function*, and more.

Starting PHP the Right Way
As I wrote earlier, PHP is a very easy language to learn, but
if you want to be a good PHP developer, you must under-
stand what’s going on, and start it the right way.
In my opinion, it’s impossible to think of developing some-
thing in PHP nowadays without using Composer and the new
language changes that came with PHP 7. Everything else you
will be able to understand by reading and writing code, like
closures, for example.
A great exercise is reading code from known frameworks,
like Laravel^18 , Symfony^19 , Zend Framework^20 , and others. In
this case, these first three use almost everything discussed
in this article. It’s easier to understand how you use all those
improvements covered above.
If I could, I would suggest you something when starting
with PHP. Start with studying everything you can about
*Object-oriented programming (OOP). It’ll be easier to
understand how to write good PHP code.
You’re probably going to find very short examples in PHP
around the web, with a single .php file that handles a lot of
logic; it’s very easy to read, by the way. This is not what you
are going to see in the market, where you have to write good
and maintainable code. So start PHP the right way! Under-
stand it first!

18 Laravel: https://laravel.com
19 Symfony: https://symfony.com
20 Zend Framework: https://framework.zend.com
Free download pdf