+

PHP 7 | Characteristics

Scalardata type Hint:Scalar data types are boolean, integer, float, and string. Previously, we could not make a type hint for a scalar data type. We made hints for array or class objects.Example : ini_set ( `display_errors` , `1` ); class User {} function test (User $abc ) { var_dump ( $abc ); } test ( new User); ?> Output:
object (User) # 1 (0) {} 
Now let`s see how to do type hinting for the scalar data type that is available in Php7 ini_set ( `display_errors` , ` 1` ) ; class User {} function add (int $a , int $b ) { return $a + $b ; } echo add (3, `34` ); ?> Output:
37 
We pass a string in the second line of the argument ` 34 `, but the output we get here is the addition of 34 and 3, which is 37, because here is strict the mode is disabled. Now, if you want to limit this, you need to advertise at the top of the file.
Example : declare (strict_types = 1); ini_set ( `display_errors` , `1` ); class User {} function add (int $a , int $b ) { return $a + $b ; } echo add (3, `34` ); ?> Output:
Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type integer, string given, called in /home/cg/root/5012177/main.php on line 11 and defined in / home / cg / root / 5012177 / main.php: 7 Stack trace: # 0 /home/cg/root/5012177/main.php(11): add (3, `34`) # 1 {main} thrown in / home / cg / root / 5012177 / main.php on line 7 PHP Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type integer, string given, called in /home/cg/root/5012177/main.php on line 11 and defined in /home/cg/root/5012177/main.php:7 Stack trace: # 0 /home/cg/root/5012177/main.php(11): add (3, `34`) # 1 {main} thrown in /home/cg/root/5012177/main.php on line 7 
Now let`s look at an example for a boolean. Below is the code for boolean data type.
Example : declare (strict_types = 1); ini_set ( `display_errors` , `1` ); class User {} function isBoolean (bool $a ) { var_dump ( $a ); }  
isBoolean (true);
?>
Output:
bool (true) 
Return type declaration:In C or C ++, define the data type of the value that will be returned from the function. Now let`s see how to do this in Php7.
Example : declare (strict_types = 1); ini_set ( `display_errors` , `1` ); function test(): array { return []; }  
var_dump (test());
?>
Output:
array (0) {} 
In the above example, after declaring the function, you must specify the data type with: and then return the same array of data types to the test() function. Null merge operator:The null merge operator is "??". This operator replaces the mostly ternary operator. Previously we used ternary operator like below & # 8230; declare (strict_types = 1); ini_set ( `display_errors` , `1` ); $name = isset ( $_ GET [ ` name` ])? $_ GET [ `name` ]: `Not Found` ; var_dump ( $name ); ?> Output:
string (9) "Not Found" 
Now with "??" Operator we can shorten the code.
Example : declare (strict_types = 1); ini_set ( `display_errors` , `1` ); $name = $_ GET [ ` name` ] ?? `Not Found` ; var_dump ( $name ); ?> Output:
string (9) "Not Found" 
Imported imports and namespaces.Previously, when we had to define multiple namespaces and classes and use or import a class from one namespaces to another namespace, the "use" keyword was used, and then we had to provide a namespace with the class name to be imported.
Example : namespace Course { class BTech { } class MTech { } } namespace CourseBranch { // CSE in both courses // BTech and MTech class CSE { } } namespace App { use CourseBTech ; use CourseMTech; use BranchCSE; var_dump ( new BTech()); var_dump ( new MTech()); }  
?>
Output:
object (CourseBTech) # 1 (0) {} object (CourseMTech) # 1 (0) {} 
In the above example, import two classes, but what if you need to use multiple classes from one namespace to another namespace. He will have to write a few lines of "use" statement defining each class, which will make your code long. Therefore, in PHP7 bulk import was introduced to solve this problem. Let`s see how the above example would work using bulk import along with a nested namespace.
Example : namespace Course { class BTech { } class MTech { } } namespace CourseBranch { // CSE is available in BTech and MTech courses class CSE { }} namespace App { use Course { BTech, MTech, BranchCSE }; var_dump ( new BTech()); var_dump ( new MTech()); var_dump ( new CSE()); }  
?>
Output:
object (CourseBTech) # 1 (0) {} object (CourseMTech) # 1 (0) {} object (CourseBranchCSE) # 1 (0) {} 
code and will become more readable and convenient.Operatorof the / spacecraft combo comparison: The combo operator& # 8212; it is a combination of three operators. These operators are less, greater, and equal. This combination of three operator runs and the output will vary depending on the result. Now let`s take an example of this operator. Example : < ? php $arr = [ `welcome` , ` to` , `engineerforengineer` ]; usort ( $arr , function ( $a , $b ) {< / code> return strlen ( $a ) < = > strlen ( $b ); }); var_dump ( $arr );  
?>
Output:
array (3) {[0] = > string (2) "to" [1] = > string (7) "welcome" [2] = > string (13) "engineerforengineer"} 
In the above example, we compare the lengths of the words defined in the array and sort them in ascending order. Usort() & # 8212; it is a built-in PHP function where passing an array as the first parameter and the second parameter & # 8212; it is a custom function meaning your own custom logic function. Learn more about the usort() function at this link usort() . So instead of using it manually with if and else, we can use a spaceship or combined operator for the same task.Anonymous classes:PHP7 introduced anonymous classes, now the question is when to use them? When you only have to use a class once and you don`t want it to be instantiated again. First, let`s see how we usually create a class and an object.Example : class test { function hiGeeks() { echo " engineerforengineer " ; } } $sayGeeks = new test; $sayGeeks -> hiGeeks(); ?> Output:
engineerforengineer 
Now let`s see how to create an anonymous class.
Example : $anonymous = new class { private $readme  = " Welcome to engineerforengineer " ; function printOut() {< / code> echo $this -> readme; } }; $anonymous -> printOut(); ?> Output:
Welcome to engineerforengineer 
Here we can create an object for this class, but we can extend another class.
Example : class test { function hiGeeks() { echo "engineerforengineer" ; } } $sayGeeks = new test; $sayGeeks -> hiGeeks(); $anonymous = new class extends test { private $readme  = " Welcome to engineerforengineer " ; function printOut() {< / code> echo $this -> readme; } }; $anonymous -> printOut(); $anonymous -> hiGeeks(); ?> Output:
engineerforengineer Welcome to engineerforengineerengineerforengineer 
Closure::call() method:PHP7 introduced a new method call(), which is a shorthand way to calling the closure when an object region is bound to it. Most of the time in JavaScript we use closures, which are anonymous functions, in PHP 5 we also use closures, but in PHP7 we use them differently.
Example : class User { private $username ; private $email ; public function __ construct ( $username , $email ) { $this -> username = $username ; $this -> email = $email ; } } $getUserEmail = function () { return $this -> username. `email address is` . $this -> email; }; $user = new User ( ` abc` , `abc @ gmail.com` );  
// Using PHP5 $email = $getUserEmail -> bindTo ( $user , `User` ); echo $email (). ` ` ;  
// Using PHP7 echo $getUserEmail -> call ( $user ); ?>
Output:
abc email address is [email protected] abc email address is [email protected] 
call() method makes our code cleaner and easier to use.< strong> Generator:Php programming has become more powerful due to some new features such as "returning" with generators and exiting in it. Let`s see how its hierarchy works in PHP7.
Example : function values ​​() {< p> yield 200; yield 300; return 500; } p> $user = new User ( `abc` , `abc @ gmail.com` );  
// Using PHP5 $email = $getUserEmail -> bindTo ( $user , `User` ); echo $email (). ` ` ;  
// Using PHP7 echo $getUserEmail -> call ( $user ); ?>
Output:
abc email address is [email protected] abc email address is [email protected] 
The call() method makes our code cleaner and easier to use.
Get Solution for free from DataCamp guru