History Of PHP
PHP was invented in 1994 by Rasmus Lerdorf. The first version of PHP was used by others in 1995 and was known as Personal Home Page Tools. It was accordant of a very simple parser engine that only understood a few special macros and some utilities that were in common use on home pages back then. That was recast in mid of 1995 and named PHP / FI v2. In the FI version, Rasmus uses some HTML data. He combined PHP with Form Interpreter and added mSQL support then PHP / FI was born.
PHP 7 advanced and making a huge performance improvement for better performance. Introducing types and other features. Now it’s time to introduce PHP 8 to continue the PHP legacy. Here, I've listed some of the best new features in PHP 8, people must read this.
New features in PHP 8
- Constructor Property Promotion
- Validation for Abstract Trait Methods
- Incompatible Method Signatures
- Arrays Starting With a Negative Index
- Union Types 2.0
- Stricter Type Checks for Arithmetic/Bitwise Operators
- Match Expression v2
- Saner Numeric Strings
- Saner String to Number Comparisons
- Nullsafe Operator
- Named Arguments
- Attributes v2
- Allows class syntax on objects
- Trailing Comma in Parameter List
- Consistent Type Errors for Internal Functions
- Throw Expression
- Weak Maps
1. Constructor Property Promotion in PHP 8
Constructor Property is only related to promoted parameters. And all parameters are prefixed with the public, protect, and private keywords. In PHP 8 most of the properties are repeated but we can also use them with an object. Those are the best new features in PHP 8.
Constructor Property helps to reduce the common code while you are creating an object. It allows combining fields, the definition of the constructor, and variable assignment all in one syntax into the constructor. Now, let's see the example.
2. Validation for Abstract Trait Methods in PHP 8
Trait methods define as a mechanism in PHP and its reuse in inheritance language. But the most used method is the declared method that can use in multiple classes. The trait can also contain abstract methods, which are used to specify the requirements the trait has on the user class. Here, I've listed one of the best new features in PHP 8. Let's see the example.
3. Incompatible Method Signatures in PHP 8
In PHP, inheritance error occurs through incompatible method signatures. If the class implementing an interface then it threw the fatal error. According to Object Interface Documentation(OID), the class implementing that must use method signature which compatible withLiskov Substitution Principle, if do not this then fatal error will occur. That is a useful new feature in PHP 8.Example of Incompatible Metod Signature:
4. Arrays Starting With a Negative Index in PHP 8
An array is just one type of ordered map. using an array you can use positive or negative integer, and string as key values. I think that's one of the best new features in PHP 8. A negative key is to have a special meaning in this language, like any other value that can be the associate array's key. Let's see the Example:
5. Union Types 2.0 in PHP 8
Union types accept different types of values. But, PHP does not support this type, it supports only? Type and iterable. And this is one of the best new features in PHP 8 Array or Traversable, using the special iterable type. It supports all available types, with some limitations. Now let's see the Unione type example:
6. Stricter Type Checks for Arithmetic/Bitwise Operators in PHP 8
The previous PHP version allows bitwise and arithmetic operators to an array or on non-overload objects. The behavior of scalar operands remains unchanged. The object takes no operator or cast overloading is involved in that cases.
7. Match Expression v2 in PHP 8
The new match expression is similar to the switch case and allowing to return values. It's easy to understand the difference between the two functions. Now, let's see the example:
8. Saner Numeric Strings in PHP 8
PHP has the wide concept of numeric string, which can be clarified as numbers. It can be categorized in threeway are as follow.
- A numeric string allows only numbers and optionally whitespace characters for example "123" or "123xyz45".
- A leading numeric string beginning with number but after followed by non-numeric characters for example "123xyz" or "12".
- A non-numeric string is a string that is not numeric or not leading.
9. Saner String to Number Comparisons in PHP 8
In the last PHP version, Strict comparison compares string with strcmp(), and non-strict use smart comparison method that count string as numbers if it is numeric. It requires the key occurring in the same orders when non-strict allow it without an ordered key. That is one of the most useful new features in PHP 8.
10. Nullsafe Operator in PHP 8
RFC introduces the Nullsafe operator $-> with short circuit evolution. The second operator has evaluated only the first operator doesn't evaluate to null. If the chain evaluates to null then the execution of the chain stops and evaluates to null. Let's see the example to get more clear.
11. Named Arguments in PHP 8
Named argument allows passing the new argument in function in PHP. It allows passing the argument to the function when it is based on the parameter name. You can pass the arguments easily using the parameter name. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily. That's one of the most useful new features in PHP 8. Now let's see the example:
Attributes are also known as annotations, which is the structure of metadata that uses specified properties for objects, elements, and files. It was added before the declaration which belongs to the document comments block. Each declaration of functions, class, property has one or more attributes.
We can use this syntax Foo\Bar::class to merge the name of the class. The RFC uses the same syntax to merge the name of the given class. In PHP 8 the same function is executed if it is not then throw the Typeerror as output. Let's see the example to understand more properly.
Trailing commas are commas that are situated in between the item that presents the sa[rate values of each item. PHP 7.2, PHP 7.3, and even PHP 8 have this beautiful feature to make readable code. That's one of the best new features in PHP 8. Let's see the example of Trailing Commas.
When you pass some illegal parameter value, the function behaves differently and the user-define function throws a Type error. A typical behavior gives a warning and returns the null. The RFC proposes consistency returns the Typeerror for invalid parameters. Let's see the example.
16. Throw Expression in PHP 8
In PHP Throw is a statement that makes it impossible to throw exceptions in places where only expressions are allowed, like arrow functions, the coalesce operator, and the ternary operator.A weak map is a collection of objects where keys are weakly referenced means they are u-not use to reduce the garbage collected. Its primary use case is for building data obtained from an object that does not require to be higher than the object. This is one of the most used new features in PHP 8. Let's see the example:
Also Read For:: New Features of JavaScript 2021
Also Read For:: 7 Themes to Make Your VS Code Look Beautiful
Tags
new features in php 8
new features of php 8
php 8 new features
php 8 performance
what is latest version of php