Programming

New in PHP 7

Scalar type-hints:

Type-hints exists in php. Unlike they were limited to classes, arrays and callable.

In php seven, you can use the scalar-types (Integers, Floating point numbers, Booleans and strings)  as type-hints.

With php seven developers are allowed and to ensure a better input consistency of a function/method interface. And by default “coercive mode” is enabled. This restricts php from giving a type-error when the types don’t exactly match, but when a conversion is still possible.

Anonymous classes:

Anonymous classes are useful for simple one-off objects. With anonymous classes you can define a class and instantiate an object inline.

The Closure::call() method:

Closures are anonymous functions that are declared inline and assigned to a variable. It can be used as a callback for later execution. In PHP 5 it was already possible to bind an object to the scope of the closure as if it was a method.

The “call” method is one of the PHP 7 features that was introduced to simplify the process.

Generator delegation:

Generators are great, but sometimes hard to illustrate. Unluckily using them is surprisingly simple. It’s all about the “yield” keyword. By using “yield”, a generator is returned with the value that is yielded. A generator implements an iterator which makes it easy to use in “while” or “for” loops.

In PHP 7 generator delegation was introduced. This means a generator from another function can be addressed.

Generator return expressions:

As satated: by using the yield keyword, values can be yielded and iterator over. But return values are ignored by generators.

In PHP 7 the “getReturn” method was added to the Generator class to allow return values in generators.

The null coalesce operator:

The null coalesce operator is a shorthand for checking if a value is set and not null within inline comparisons. Instead of doing the same old “isset” check over and over again, just use “??” to return the value if it is set (and not null) or an alternative value instead.

The space ship operator:

The so called “space ship operator” makes it easier to compare values. Instead of returning a typical true/false value, the space ship operator returns one of the follow values based on the result of the evaluation:

0 when both values are equal

-1 when the left value is less than the right value

1 if the left value is greater than the right value

Throwables:

A immense change in PHP 7 is the fact that errors are no longer raised the way they used to be raised. Errors now behave in a similar way as exceptions. They both inherit from the Throwable interface.

This means that errors can now be caught in a try/catch block. You can catch both exceptions and errors as Throwables, but you can also catch errors as Error objects.

Following are event different kinds of errors we can catch:

Level support for the dirname() function:

The dirname function is used more often than you would think. It is the ideal function to refer to directories in a relative way.

But when you want to go a couple of levels up, you end up nesting dirname calls and that will eventually lead to confusion.

As of PHP 7 the dirname function has a second argument that indicates how many levels you’re going up the directory tree. If you don’t enter a value, 1 is the default.

The Integer division function:

Maybe not one the most important PHP 7 features, but still worth mentioning: the intdiv function returns the integer value of a division whereas regular divisions can result in a float being returned.

Uniform variable syntax:

In PHP 7 the “uniform variable syntax” was introduced. This standard changes the way indirect access to array keys, properties and methods is evaluated. The PHP 7 interpretation enforces a strict left-to-right evaluation.

Performance:

It will be good to mention that the performance of PHP 7 is supposed to be impressive. Some say that it’s twice as fast as PHP 5(.6) others use even more dazzling numbers.

 Developer community says that it even outperforms HHVM.

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply