PHP7: New Release Pandas PHP7

0

PHP Pandas: PHP7

PHP7 has released with some major positive changes it will arrive with a fully-stocked toy-box for us to experiment. It worth noting that PHP7 has been benchmarked at executing code up to double as fast as previous versions of PHP. This is a speed increase that we cannot turn down!

Mostly people are using PHP5 so it is better for those to give a quick jump to latest technology and enhance their existing skills to higher. This blog will help you to understand the values of PHP7 in your coding.

Let’s dive into the ocean of PHP7 ūüėČ

Installation

It could be a little bit tricky this time! You see, while writing this blog PHP7 has yet to be released. Third-party package channels are distributing the release candidate of the new version, but if you’re not satisfied by third party sources then I’d stick with PHP5 until the new version is released. Once the final version has been released, I will come again to update this blog.

Where is PHP6?

Good question! It has been lurking in the same place as Windows 9, unfortunately.

Simply put, there was a version of PHP known as PHP6, but it was not as better for commercial use. To repel the confusion, PHP internals team decided to use version 7 as the next release milestone for the project. This proves that there will never be a good PHP 6.

If you need more reasons, I suggest taking a look at the PHP 6 vs 7 RFC where a vote was cast to decide upon the version number for the latest iteration of the language.

Return Type Declarations

I have choose this new feature, and I assure you that if you will read you would also love that.

In the recent versions of PHP, functions and methods were able to return values of any type that they desire. PHP is a loosely-typed language, and as such we have the option of returning whatever variable type pleases us!

Don’t get panic! This is now possible in PHP7. However, we have the option to be efficient about how our functions and methods will return values. I think this is a great place to note that the PHP internals team have done a wonderful job of keeping PHP7 highly backward compatible.

Let’s take a look at a new way of defining a function in PHP7.

<!--?php function pandaSeven() : int { return 3; } ?-->

Sacred moly, would you investigate that! A radical new configuration. Should we investigate the method comparable?

&lt;?php
 
 
class Panda
 
{
 
public function roar() : string {
 
return 'Mew!';
 
}
 
}

Wonderful!!!

By including a: and a variable sort, we’ve told PHP that the function that we’ve defined must give back a return of the defined type. This implies the case above will work fine and dandy, however how about we look into another function.

&lt;?php
 
 
class Panda
 
{
 
public function roar() : string {
 
return 4.5;
 
}
 
}
 
 
$panda = new Panda;
 
 
var_dump($panda-&gt;roar());

Said example, the return type of the roar() method is defined as a string, but if you look very closely, you will observe that we are returning a float value instead.

What will happen if we execute the code snippet above?Do you have any idea? Let try this!

string(3) "4.5"

Indeed, that is not all that substantial is it? You see, PHP knows that our method must return a string, and it’s fully capable of casting a float to a string, so that is the thing that it does before returning it.

Truth be told, any sort which PHP can cast, will be rectified when the value is returned.

&lt;?php
 
 
class Panda
 
{
 
public function roar() : string {
 
return [3, 5];
 
}
 
}
 
 
$panda = new Panda;
 
 
var_dump($panda-&gt;roar());

Great! How about we execute the scrap to perceive how PHP7 adapts in this situation.

Fatal error: Uncaught TypeError: Return value of Panda::roar() must be of the type string, array returned in <file>:7

PHP doesn’t know how to cast that value to a string, but we tell the language that the method¬†must¬†return a string. This will definitely throw an error!

Before that, we noticed that how PHP will cast a float to a string to meet the requirements of the return type declaration. What happens if we don’t need this functionality? In fact, many other 'strict' languages will not cast for you. They will simply throw an error if the return type is not correct.

Luckily! This is also possible with PHP7, but we must first enforce 'strict' type checking.

You may only enable strict type checking on a file-by-file basis. This means that you will have to enable it for every file in which you had like to use strict types.

To use strict types, the following declaration must be the first snippet of PHP code to appear after the opening¬† < ? php ¬†language brace. For instance…

&lt;?php declare(strict_types = 1);
 
 
class Panda
 
{
 
public function roar() : string {
 
return 3.5;
 
}
 
}
 
 
$panda = new Panda;
 
 
var_dump($panda-&gt;roar());

Here, we mentioned declare(strict_types = 1); on the first line of the file, so we can be sure that PHP is not going to try and be clever about return types. Our method should return a string, but we are returning a float. What will happen, do you have any idea?

Fatal error: Uncaught TypeError: Return value of Panda::roar() must be of the type string, float returned in <file>:6

Amazing! No more mister smartypants PHP!

So I wonder, why is this feature useful? We could just be extra careful about our return types, couldn’t we?

In reality, the feature is not all that useful by itself, but when used with an interface or abstract class, new possibilities present themselves!

In a last chapter, we explained how defining our method signatures in an interface or abstract class, will build trust with classes which implement/extend them. Well, the return type declaration is also part of the method signature.

This shows that if we have an interface, for example.

&lt;?php
 
 
interface BambooEater
 
{
 
public function roar() : string;
 
}

We know that any class which implements this interface and the roar()method must return a string. Thus, we will not have to check the type of the value returned before using it. Yet another contract has been made.

You can declare the return type of a function or method to be of any scalar or non-scalar type, including classes. This also means that you can set the return type to be of a specific interface, and experience polymorphism within the implementations returned from the function!

Amazing! I would love to see what this new feature will bring to the world of PHP.

Scalar Type Declarations

In my last post, I inform you that type-hinting methods and functions with scalar types (int, string, etc) was not possible? WAO…! Forget it!

Using PHP7 you can type hint…¬†ANYTHING.

Impressed? I know you are!

&lt;?php
 
 
function pandaSeven(int $seven, string $lushui) {
 
return $seven;
 
}

This is obviously true with PHP7.

It is not important that as with return type declarations, PHP will attempt to cast a value into the defined type. Again, using the declare(strict_types = 1); strict type declaration will cease this behavior, and cause an error to occur.

Null Coalesce Operator

In old versions of PHP, we had often found ourselves doing something similar to the following.

&lt;?php
 
if (isset($panda)) {
 
echo $panda;
 
} else {
 
echo 'Giant Panda';
 
}
 
// or...
 
echo isset($panda) ? $panda : 'Giant Panda';

This is due to that we don’t want to attempt to access a variable that is not set. We really don’t want an error on our hands. We also don’t want it to be null.

With PHP7, we have the Null Coalesce ?? Operator, which looks like that.

&lt;?php
 
echo $panda ?? 'Giant Panda';

If the value to the left of the¬†??¬†doesn’t exist, or is null, then the statement will use the value to the right of the operator. Aha! It is short, isn’t it?

Not only that, but you can stack as many of these operators into a statement as you wish. e.g.

&lt;?php
 
echo $first ?? $second ?? $third ?? $fourth ?? 'fifth';

Here, PHP7 will work its way from left to right using the first value which is valid. I am sure that you would love this feature.

Spaceship Operator

Spaceship operator makes it really simple to compare values. For an example.

&lt;?php
 
$tom = 3 &lt;=&gt; 4;

As you can see, the spaceship <=> operator looks like a spaceship.

In the above example the value of¬†$tom¬†will be set to¬†-1. This is due to that 3 is less than 4. Let’s take a look at the possible values.

$a &gt; $b : +1
 
$a = $b :  0
 
$a &lt; $b : -1

This format of the resulting value is useful. That‚Äôs why many of PHP’s native sorting functions use the format to determine the order of values.

Define Constant Arrays

We already know that how to define global constants. For example.

&lt;?php
 
define('PANDA', 'Awesome!');

Well, in PHP7 you can also define constants that are arrays. Here is an example.

&lt;?php
 
define('PANDA', ['Awesome!', 'Great!', 'Fluffy!']);
 
echo PANDA[0];

Very simple! Well what did you expect? Not all of the features are going to be major, are they!?

Anonymous Classes

Anonymous classes are those classes that are without their names. Shocked? No? Oh, ok..

Here is an example.

&lt;?php
 
$panda-&gt;eat(new class {
 
public function noise()
 
{
 
return 'Crunch!';
 
}
 
});

In the said example, we are passing an anonymous class into a function. It has no name did you see that?

Anonymous classes are useful for when you don’t want to keep a reference (assignment to a variable) to the class. These types of classes are extremely useful when combined with unit testing, which is a concept that will be covered in a future title!

Group Use Declarations

Let’s take a look at Group Use Declarations.

Before that, to use multiple classes held within the same namespace we’d have to¬†use¬†them a partly, like this.

&lt;?php
 
namespace OtherNamespace;
 
use Animal\Panda\Red;
 
use Animal\Panda\Giant;
 
use Animal\Panda\Fluffy;

With PHP7, you have got another option. Here is an example.

&lt;?php
 
namespace OtherNamespace;
 
 
use Animal\Panda\{Red, Giant, Fluffy};

The impact is indistinguishable, yet you’ve spared yourself a couple of keystrokes. It is worthless that you can still use class aliases within the group¬†{¬†use¬†}¬†brackets. For example.

&lt;?php
 
namespace OtherNamespace;
 
use Animal\Panda\{Red as Lushui, Giant, Fluffy};

While some will want to utilize this format. I would really support the utilization of the “more established” methods for utilizing multiple classes inside the same namespace. If using a rendition control framework, it will be much simpler to figure out if a class is evacuated, or to submit a change without happening conflicts with group-worker code. I additionally believe it’s much clearer to peruse!

We’ve not secured rendition control in this title, so don’t freeze if the above explanation doesn’t bode well! Use whichever group functions admirably for you. It’s incredible to have choices!

Share.

About Author

Leave A Reply