Posts Tagged ‘anonymous class’

New Features in PHP 7

Wednesday, May 2nd, 2018

Programming language and the programmers all over the world experienced a new wave of change when PHP7 launched. This web-based application is compatible with mobile, enterprises solutions, and cloud platforms. However, there are a lot more to learn about this version to get comfortable while programming. It has got two new operators and many more exciting changes to ease the PHP lovers around the globe. Let’s dig out what are those to get a quality knowledge supplement.

Speed:

First and foremost thing that immediately gets noticed is its speed. PHP7 has an almost double speed of operation as that of PHP 5.6. Regarding consistency, it stands high as compared to its earlier versions. This means you will gain more proficiency than before as unwanted time lags will not be there.

Improved open source scripting:

PHP5 was using Zend Engine II at its heart, but PHP7 has got an upgraded version, Zend Engine III. It can significantly improve the performance level. As it has been written in C that interprets PHP, the functionality gets enhanced. It comes under the code name of PHPNG (Next Generation). This ensures that PHP7 is a must-try affair for every programmer around the globe.

Manual Variable declaration:

PHP 7, unlike its previous shades, enables you to specify your desired variable through its Type Declaration feature. It not only makes your code easy to read but also enables you to make small changes to variables without bringing any effect to the overall result. Earlier, this feature was not present and PHP was automatically setting up the variables. This changeover need not require you to declare data types as variables will still have them attached. However, you can make some radical changes. For instance, you can add a string to an integer, and it won’t result in any programming error.

For example,

Function abx(): array {

Return [ ];

}

Consider the example shown above. It defines array type of variable. With return type declaration used above, the function will return only the array. You can use this logic for other returns types such as int, float, and bool. Thus, being a weakly typed language, PHP7 can help you enhance the quality of the code.

Handling Fatal Errors:

A slight mistake from your side and your script may get into a fatal error. This is a common problem that every programmer experiences and previous PHP versions used to stop the script without involving the error handler. However, PHP 7 gives a little relief. An exception will be thrown instead of closing the entire script down.

Easy User-land CSPRNG:

It is nothing but an application space present outside the Kernel. Easy User-land CSPRNG can be covered by privilege separation. This API enables you to use PHP 7 with ease. It is reliable and is used to generate cryptographically secure pseudo-random numbers. No doubt, these random data were also present in the earlier versions, but none of them were as secured at this time. The importance of random numbers goes primarily while designing the program for generating frequently changing random passwords.

Introduction of two new operators:

If you’re upgrading to PHP7, then you will get to know about two new operators; Spaceship operator (<=>) or Combined comparison and Null Coalesce Operator. Let’s get to know about these operators in brief.

Spaceship operator (<=>) or Combined comparison

This one has got behavior similar to strcmp() or version_compare(), but the good thing is that all generic PHP values can get utilized through this new operator in combination with usual semantics like <, <=, ==, >=, >, etc. The comparison rules used is same as that of the previous semantics.

This spaceship operator has got simple usage funda. This operator “(expr) <=> (expr)” will fetch you a value ZERO if both operands are of same value. If the left side is greater than the right side, you will get ONE and -ONE for vice versa condition.

To understand about its behavior better, see how it returns results in case of the followings:

//integers

Echo 11 <=> 11; //0

//Floats

Echo 11.5 <=> 12.5; //-1

//Strings

Echo c <=> b; //1

Use usort() along with this operator to make the ordering easier.

Null Coalesce Operator 

“??” this is the symbol of Null Coalesce operator. If you want to assign a value to nullable or reference type files, this one can be your savior. If the operand is not null, the operator will return you left-hand operand, and if the reverse is the case, you will get right-hand operand in return.

In PHP7, without engaging this operator, if you are trying to assign a nullable value to a non-nullable one, you will get compile time error for sure. However, the use of “??” operator won’t let that happen.

See the below example for more clarity.

Int? a = null; //here a is a nullable value type

Int x = 0; //here a is a non-nullable value type

X = a;

You will get compile time error as below:

Cannot implicitly convert “int”; to “int.” An explicit conversion exists.

Anonymous class:

A new entry to PHP7 is “anonymous classes.” These classes have no name and can be defined simply with a keyword “new class.” The functionality goes in the similar line as that of an object of named class. They have internally generated names so, we need not define them again. They are useful for creating one-off objects.

For example, consider the below one;

Anonymous class_Image

Earlier, for creating a new class, it was required to override a property or method. That was extending the named class. However, with Anonymous class, your job will be easy for creating a simple one-off object.

Namespace:

If you want to encapsulate the items, then Namespace is what you will be looking for in PHP7. It can import a group of classes, functions, and constraints from the same namespace.

See this example below.

Before PHP7, you need to work as below:

Use xyz\abc\class Z

Use xyz\abc\class Y as newcode

After PHP7, you can work as below:

Use xyz\abc\ {class Z, class Y as newcode};

Here, we have highlighted the most important ones. However, there is a long list of the same. There are few technical advantages also. For instance, it is a commonly known fact that Linux is a more favorable playground for PHP rather than Windows and until now, it was running smoothly on 32-bit Windows, but the current version supports 64-bit windows as well. Therefore, with so much of changeovers, PHP7 can be quoted as one of the major release after PHP5 since 2004. Try to keep digging this new platform to gain maximum output with minimum input.