More Related Content Similar to What is new in PHP (20) More from Haim Michael (20) What is new in PHP1. What is new in PHP 8
Haim Michael
March 24th
, 2021
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
life
michae
l
Let's be on The Edge
www.lifemichael.com
3. © 2008 Haim Michael 20150805
Union Types
Union types is a collection of two or more types that either
one of them can be used.The void type can never be part of a
union type. The void type indicates that there isn't a returned
value.
<?php
class A
{
public function foo(X|C $input): Z|W|U
{
}
}
?>
4. © 2008 Haim Michael 20150805
JIT Compiler
The virtual machine was improved. JIT (Just In Time)
capabilities were added.
JIT virtual machines continuously analyze the code being
executed and identify parts worth additional compilation.
5. © 2008 Haim Michael 20150805
The Nullsafe Operator
When having a variable that might hold the null value we
can avoid the if statement when using that variable for
calling a method and use the null safe operator instead.
$result = $ob ? $ob->getMagic() : 99;
6. © 2008 Haim Michael 20150805
The Nullsafe Operator
<?php
class Person {
private $magic;
function __construct($num) {
$this->magic = $num;
}
function getMagic() {
return $this->magic;
}
}
function findPerson() {
//...
return null;
}
$ob = findPerson();
$result = $ob ? $ob->getMagic() : 99;
echo $result;
?>
7. © 2008 Haim Michael 20150805
Arguments Naming
When calling a function we can specify for each and every
argument we pass the name of the parameter it targets.
8. © 2008 Haim Michael 20150805
Arguments Naming
<?php
class Utils
{
public static function total(int $num1, int $num2):int
{
$result = $num1 + $num2;
return $result;
}
}
$a = 3;
$b = 6;
$sum = Utils::total(num1:$a,num2:$b);
echo $sum;
?>
9. © 2008 Haim Michael 20150805
Attributes
We can now use attributes (AKA decorators and annotations
in other programming languages) and develop new ones.
<?php
#[OurAttribute]
class Foo
{
#[OurAttribute]
public const FOO = 'foo';
#[OurAttribute]
public $x;
#[OurAttribute]
public function f(#[OurAttribute] $bar) { }
}
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD |
Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
class OurAttribute
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
}
?>
10. © 2008 Haim Michael 20150805
Attributes
We can now use attributes (AKA decorators and annotations
in other programming languages) and develop new ones.
<?php
#[OurAttribute]
class Foo
{
#[OurAttribute]
public const FOO = 'foo';
#[OurAttribute]
public $x;
#[OurAttribute]
public function f(#[OurAttribute] $bar) { }
}
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD |
Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
class OurAttribute
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
}
?>
11. © 2008 Haim Michael 20150805
Match Expression
We can now use match expressions instead of the well known
switch statements.
<?php
function f($num) {
$result = match($num) {
0 => "Zero",
1 => "One",
'1', '2', '3' => "Number",
default => "Unknown"
};
return $result;
}
$temp = f(77);
echo $temp;
?>
12. © 2008 Haim Michael 20150805
Constructor Properties Promotion
We can now define a constructor and add access modifiers to
its parameters. Doing so will promote the paremeters into
properties.
13. © 2008 Haim Michael 20150805
Constructor Properties Promotion
<?php
class Rectangle {
function __construct(
private int $width,
private int $height) {}
function area() {
return $this->width * $this->height;
}
}
$ob = new Rectangle(3,4);
echo $ob->area();
?>
14. © 2008 Haim Michael 20150805
The static Return Type
We can now define a method that its return type is static.
When calling such method it is possible to use its returned
value for calling static methods.
15. © 2008 Haim Michael 20150805
The static Return Type
<?php
class Foo
{
public function a(): static
{
echo "a";
return new static();
}
public static function b(): static
{
echo "b";
return new static();
}
}
(new Foo())->a()::b();
?>
16. © 2008 Haim Michael 20150805
The mixed Type
We can define a function specifying that its returned value is
of the type mixed. Doing so, it would be possible to return
any of the following types: array, bool, callable, int, float, null,
object, resource and string.
function f(): ?mixed {}
17. © 2008 Haim Michael 20150805
The Throw Expression
The throw statement has become an expression. It is now
possible to throw exceptions in many new places.
18. © 2008 Haim Michael 20150805
Private Methods Inheritance
When extending another class, that already includes a
specific private methods, we can now declare that method
with a different signature.
19. © 2008 Haim Michael 20150805
Private Methods Inheritance
<?php
class A {
private function f($num1) {
return 10*$num1;
}
}
class B extends A {
private function f() {
echo "Bex";
}
function g() {
$this->f();
}
}
(new B())->g();
?>
20. © 2008 Haim Michael 20150805
The WeakMap Class
We can now enjoy the WeakMap class when handling heavy
objects that relatively consume more resources.
The WeakMap class was built upon the WeakRefs RFC. The
WeakMap object holds references to objects, that don't
prevent them from being garbage collected.
21. © 2008 Haim Michael 20150805
The WeakMap Class
<?php
class VideoClip {
function __construct(private string $title) {}
function __toString() {
return "book... ".$this->title;
}
}
class ID {
function __construct(private int $id) {}
}
$wm = new WeakMap();
$k = new ID(123);
$wm[$k] = new VideoClip("Matrix 1");
echo $wm[$k];
?>
22. © 2008 Haim Michael 20150805
The ::class Class
We can now use the ::class feature on objects (so far it was
possible on classes only).
23. © 2008 Haim Michael 20150805
The ::class Class
<?php
class VideoClip {
function __construct(private string $title) {}
function __toString() {
return "book... ".$this->title;
}
}
$v = new VideoClip("Matrix 1");
echo VideoClip::class;
echo $v::class;
?>
24. © 2008 Haim Michael 20150805
Catchless Catches
We can now omit the variable when catching an exception
using the catch block.
<?php
try {
//...
} catch (MyException) {
//...
}
?>
25. © 2008 Haim Michael 20150805
The Stringable Interface
Using this new type we can type hint anything that
implements the __toString() method.
26. © 2008 Haim Michael 20150805
The str_contains Function
Using this function we can check whether a specific string
contains another specific string. This new function allows us
to avoid the strpos() function when checking whether one
string contains another one.
27. © 2008 Haim Michael 20150805
The str_contains Function
<?php
$a = "love";
$b = "we love php";
if (str_contains($b,$a)) {
echo "love";
}
?>
28. © 2008 Haim Michael 20150805
Traits with Abstract Methods
Till PHP 8 it was possible to define a trait that includes an
abstract method but the compiler didn't check whether that
class that uses the trait does indeed implement the abstract
method.
As of PHP 8, the compiler does verify that the class
implements the abstract methods coming from the trait.
29. © 2008 Haim Michael 20150805
Traits with Abstract Methods
<?php
trait Flyable {
abstract public function fly(int $input): void;
}
class UsesTrait
{
use Flyable;
public function fly($input)
{
//..
}
}
?>
30. © 2008 Haim Michael 20150805
The ext-json Extension Availability
As of PHP 8, the ext-json extension is always available. There
is no need to check whether it was already installed... or not.
31. © 2008 Haim Michael 20150805
Consistent Type Errors
As of PHP 8, internal functions will throw error (instead of
warning messages or returning null), the same as user
defined functions behave.
32. © 2008 Haim Michael 20150805
Engine (Warnings) Errors
As of PHP 8, many of the PHP engine warnings have become
errors.
33. © 2008 Haim Michael 20150805
The @ Operator
As of PHP 8, the @ operator no longer silences fatal errors.
This small change might reveal errors that were hidden so far.
34. © 2008 Haim Michael 20150805
Concatenation Precedence
As of PHP 8, when printing to the screen an expression that
includes the use of the mathematical operator +, the
mathematical operator + will take precedence over the string
concatenation . operator.
<?php
$a = 3;
$b = 4;
echo "sum is ".$a+$b;
?>
35. © 2009 Haim Michael All Rights Reserved 35
Questions & Answers
Thanks for Your Time!
Haim Michael
haim.michael@lifemichael.com
+972+3+3726013 ext:700
+972+54+6655837 (whatsapp)
life
michael