« Previous 1 2 3 4 Next »
New features in PHP 8
Conversion Work
Small Matters
Sorting functions in PHP have not been stable thus far, leaving the order of identical elements in the sorted results to chance. In PHP 8, all sorting routines adopt identical elements in the order that existed in the original array.
The PHP developers have also introduced a new Stringable
interface that automatically implements a class if it offers the __toString()
function. The string|Stringable
union type then accepts both strings and objects with the __toString()
method. This in turn is intended to improve type safety.
Since the JSON data format is now the format of choice in many web applications, PHP 8 can no longer be compiled without the JSON extension. Developers can more easily convert DateTime
and DateTimeImmutable
into each other using DateTime::createFromInterface()
and DateTimeImmutable::createFromInterface()
. static
can also be used as the return type (Listing 12).
Listing 12
static
class Foo { public function create(): static { return new static(); } }
Traits
Developers can use traits to smuggle functions into classes without paying attention to the inheritance hierarchy [4]. Listing 13 shows an example of this: A trait can also define abstract functions, which the individual classes must implement in turn. PHP 8 is the first version to check function signatures. The implementation of the Circle
class thus throws an error due to the wrong string
(Figure 3).
Listing 13
Traits
trait Coordinates { abstract public function area(): int; } class Rectangle { use Coordinates; public function area(): int { [...] } } class Circle { use Coordinates; public function area(): string { [...] } }
Elsewhere PHP is more agnostic: Until now, the interpreter applied some inheritance rules to private methods, even if they were not visible in the derived class. In PHP 8 this no longer happens, which means that the code shown in Listing 14 now runs without an error message.
Listing 14
Inheritance Theory
class Foo { final private function calc() { [...] } } class Bar extends Foo { private function calc() { [...] } }
Changes
PHP 8 irons out some inconsistencies and breaks backwards compatibility. For example,
0 == "foo"
is now considered false. PHP 8 only evaluates the .
operator for concatenating strings after an addition or subtraction. Code such as
echo "Length: " . $y - $x;
is now interpreted by PHP 8 as
echo "Length: " . ($y - $x);
Namespaces may no longer contain spaces in their names, but from now on reserved keywords are also allowed as parts of a namespace identifier.
Within the Reflection API, the signatures of some methods have changed. For example, instead of ReflectionClass::newInstance($args);
, PHP 8 uses the ReflectionClass::newInstance(...$args);
method. However, if you want the PHP code to run under PHP 7 and 8, the PHP team recommends using the notation shown in Listing 15.
Listing 15
Reflection Class Notation
ReflectionClass::newInstance($arg = null, ...$args);
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.