Last active
February 21, 2019 07:34
-
-
Save GideonBabu/11ce53d254b8c7f0062ed3b4b85a058a to your computer and use it in GitHub Desktop.
PHP 7 features and brief notes
This file contains 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
Better performance - PHP 7 is twice as fast as PHP 5.6. Optimization | |
Scalar type declarations - pass function params with variable type | |
function sum (int $a, int $b){ | |
} | |
Return type declarations - mention the return variable type of a function | |
function sum ($a, $b): int{ | |
} | |
combined comparison operator - <=> | |
compares two values return | |
-1 if left value is lesser | |
0 is equals | |
1 left value is greater | |
+++ | |
Null coalescing operator - ?? - returns first value if that exists and not NULL | |
$value = $user_value ?? $default_value; | |
echo $page_title ?? "My Cool PHP App"; | |
$username = $_POST['username'] ?? 'guest'; | |
+++ | |
Anonymous classes - useful for throw away classes - just for one time use class | |
new class { ... } | |
$quick_deck = new class (52) { | |
private $card_count; | |
public function __constructor($num){ | |
$this->card_count = $num; | |
} | |
} | |
+++ | |
Array as constants | |
// Define constants in PHP | |
define('APP_VERSION','1.0'); // defined at run time | |
// PHP 5.3 | |
const APP_VERSION = '1.0'; // defined at compile time - bit faster - cons - can't use conditionall value as its assigning in compile time | |
// PHP 5.6 | |
const APP_DIRS = array('app','incl','lib'); | |
// PHP 7 | |
define('APP_DIRS', array('src','lib','vendor')); // allowed in PHP 7 but not in PHP 5 | |
+++ | |
Unicode codepoint escape syntax "\u{ codepoint }" | |
echo "\u{002764}"; // print black heart symbol | |
echo "\u{260F}"; // print telephone | |
echo "\u{1F600}"; | |
+++ | |
Uniform variable syntax - Evaluate dynamic variable names left to right | |
$name = 'car'; | |
$car = array ('name' => 'Jetta', 'year' = > 2018); | |
echo $$name['name']; | |
// PHP 5: Warning: Illegal string offset 'name' | |
// Evaluated as ${$name['name']} | |
// PHP 7: Jetta | |
// Evaluated as ($$name)['name'] | |
+++ | |
cachable exceptions and syntax - no need to throw exceptions to catch errors | |
function hello_from($user) { | |
return $user->say_hello(); | |
} | |
try { | |
echo hello_from(NULL); | |
} catch (Error $e) { | |
echo "Rescued from: {$e->getMessage()}"; | |
} | |
// PHP 7: Rescused from: call to a member function say_hello() on null | |
+++ | |
PHP Error Hierarchy | |
Throwable | |
Error | |
ArithmeticError | |
DivisionByZeroError | |
AssertionError | |
ParseError | |
TypeError | |
Exception | |
LogicException | |
RuntimeException | |
+++ | |
Integer division with intdiv() | |
// PHP 7 | |
echo intdiv(5,2); | |
// 2 | |
// PHP 5 & earlier | |
echo (int)(5 / 2); | |
echo floor(5 / 2); | |
Modulus operator returns remainder | |
echo 5 % 2; | |
// 1 | |
+++ | |
Filtered unserialize() - unserialize($string, $options) | |
here $options = array('allowed_classes' => array('User')); | |
It has options to add additional params to either true or false to allowed_classes or array of allowed classes that can be serialized. | |
+++ | |
Grouped imports with use(): | |
Now, in PHP 7, we can use single use to add multiple files from a single directory | |
require_once('constants.php'); | |
require_once('database.php'); | |
require_once('plurals.php'); | |
// PHP 5 | |
use App\Library\Constants; | |
use App\Library\Database as DB; | |
use App\Library\Plurals; | |
// PHP 7 | |
use App\Library\{Constants, Database as DB, Plurals}; | |
+++ | |
Other less frequently used features in PHP 7: | |
CSPRNG library - random_bytes() and random_int() | |
CSPRNG - Cryptographically Secure Sudo Random Number Generator | |
New IntlChar class provides utility functions for working with Unicode characters and codepoints | |
session_start() - accepts an array of options which overrides php.ini file configuration | |
Closure::call for binding and calling a closure | |
assert() - can throw custom exceptions; can configure to be ignored (e.g., in production) | |
preg_replace_callback_array() - similar to preg_replace_callback() but executed per pattern. | |
Generator::getReturn() - allows returning final value from a generator | |
Generators can delegate to other generators using yield from | |
New reflection classes - ReflectionGenerator and ReflectionType | |
New reflection methods in ReflectionParameter and ReflectionFunctionAbstract | |
+++ | |
Deprecations and Deletions | |
PHP 4 Constructors - The class and method has the same name | |
// old way of constructor function | |
Class User { | |
public $name; | |
function user($name){ | |
$this->name = $name; | |
} | |
} | |
// new way of constructor function | |
Class User { | |
public $name; | |
function __construct($name){ | |
$this->name = $name; | |
} | |
} | |
+++ | |
Alternative PHP Tags: | |
<?php echo "PHP Tags"; ?> - Standard way - recommended | |
<% echo "ASP-style tags"; %> - removed in PHP 7 | |
<%= "ASP-style tags with echo"; %> - turn on PHP parser and echo the output | |
<script language="php"> - removed in PHP 7 | |
echo "Script-style tags"; | |
</script> | |
// Does not change "short-open" tags | |
<? echo "Short-open tags"; ?> | |
<?= "Short-open tags with echo"; ?> | |
+++ | |
Time Zone Warnings - removed | |
PHP 5: Date and time functions emit warnings if date.timezone is not set in php.ini; defaults to UTC | |
PHP 7: Date and time functions emit NO warnings if date.timezone is not set in php.ini; defaults to UTC | |
+++ | |
PHP 5 Deprecations and removed completely in PHP 7: | |
All ereg_* functions; use preg_* instead | |
All mysql_* functions; use mysqli_* or OOP instead - deprecated since php 5.5. | |
Magic quotes functions | |
In PHP 7, you can no longer call non-static methods inside an object statically. This is really bad programming. These were deprecated in php 5.6. | |
set_socker_blocking | |
mcrypt_generic_end | |
mcrypt_ecb, mcrypt_cbc, mcrypt_cfb, mcrypt_ofb | |
datefmt_set_timezone_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment