Created
June 13, 2019 05:17
-
-
Save junaidtk/fccd6ca0a9eedd418f7670e144fd088a to your computer and use it in GitHub Desktop.
Noticeable features in PHP7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1)Constant Array | |
=============== | |
Now the constant data types in php can be defined as numerically indexed array and associative array. | |
So below line of code can be coded like as new constant array. | |
old | |
------ | |
<?php | |
define('COLOR_RED', '#f44141'); | |
define('COLOR_BLUE', '#4286f4'); | |
define('COLOR_GREEN', '#1ae01e'); | |
define('COLOR_PURPLE', '#f309f7'); | |
define('COLOR_ORANGE', '#ef7700'); | |
New Way | |
------ | |
<?php | |
// As an associative array | |
define('COLORS', [ | |
'red' => '#f44141', | |
'blue' => '#4286f4', | |
'green' => '#1ae01e', | |
'purple' => '#f309f7', | |
'orange' => '#ef7700', | |
]); | |
echo(COLORS['red']); // #f44141 | |
// As indexed array | |
define('COLORS', [ | |
'red', | |
'blue', | |
'green', | |
'purple', | |
'orange', | |
]); | |
echo(COLORS[0]); // 'red' | |
2) Group use declaration | |
========================= | |
Previously we need to use single statement for importing class, function or variable like below code, | |
<?php | |
use VendorName/LibraryName/ClasName1; | |
use VendorName/LibraryName/ClasName2; | |
use VendorName/LibraryName/ClasName3; | |
Now we can use latest grouping method as seen below | |
------------------------------------ | |
<?php | |
use VendorName/LibraryName/{ClasName1, ClassName2. ClassName3}; | |
3)NULL Check operator | |
==================== | |
PHP will throw an error about an undefined variable, index, if the variable is not defined or empty. | |
so we need to check below code for safe code running. | |
Old method | |
---------- | |
<?php | |
if(!isset($_GET['key'])) { | |
$key = 'default-value'; | |
} else { | |
$key = $_GET['key']; | |
} | |
We can use ternary operator to check this null case. but even we need to add isset function in ternarry | |
PHP now introduced the new " Null Coalescing Operator ( ?? ) " to check the null case. | |
New method | |
---------- | |
<?php | |
$key = $_GET['key'] ?? 'default_value'; | |
You can also use below code for chained checking | |
<?php | |
if (isset($_GET['key']) { | |
$key = $_GET['key']; | |
} else if(isset($_POST['key'])) { | |
$key = $_POST['key']; | |
} else { | |
$key = 'default value'; | |
} | |
// Versus | |
$key = $_GET['key'] ?? $_POST['key'] ?? 'default value'; | |
4)Return Type Declarations and Coercive for PHP function: | |
================================================ | |
This will force the function to accept the argument type and return type of the function. | |
eg: | |
<?php | |
function reverseString(String $str) : String { | |
return strrev($str); | |
} | |
print(reverseString(1234)); | |
The above function will accept only string value and returns the string value. | |
Now Here the number 1234 is passed it will coerced into string '1234' and returns string. | |
As analogus to strict used in the javascript, in PHP we can declare the strict method for function. | |
Eg below. | |
<?php | |
declare(strict_types=1); | |
function reverseString(String $str) : String { | |
return strrev($str); | |
} | |
print(reverseString(1234)); | |
This will check the argument is string and if not report a fatal error and stop execution. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment