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 { [...] }
}
Figure 3: Here, PHP 8 has detected that the Circle class implements the area() function incorrectly.

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);

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Pandas: Data analysis with Python
    The Python Data Analysis Library, or Pandas, is built on top of the fast math library NumPy and makes analysis of large volumes of data an easy and efficient experience.
  • Modern Fortran – Part 2

    Fortran 90 was only the start. The next two iterations – 95 and 2003 – pulled Fortran into a new era of programming languages.

  • Data Analysis with Panda

    The Python Data Analysis Library, or Pandas, is built on top of the fast math library NumPy and makes analysis of large volumes of data an easy and efficient experience.

  • Innovations in PowerShell 5
    Windows 10 brings an updated, fifth release of PowerShell that vastly simplifies the task of managing modules and software packages. The scripting language now also handles various formatted output from commands and selection lists.
  • CoffeeScript: A Replacement for JavaScript?

    Through the years, many languages have tried to improve or even replace JavaScript, including CoffeeScript, which runs in all JavaScript Runtime Environments and compiles into JavaScript.

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



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.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=