mirror of
https://github.com/php/web-php.git
synced 2026-03-23 23:02:13 +01:00
* Enhancement: Enable and configure trailing_comma_in_multiline fixer * Fix: Run 'make coding-standards'
505 lines
18 KiB
PHP
505 lines
18 KiB
PHP
<?php
|
||
$_SERVER['BASE_PAGE'] = 'releases/8.0/en.php';
|
||
include_once __DIR__ . '/common.php';
|
||
|
||
releases\php80\common_header(
|
||
'PHP 8.0 is a major update of the PHP language. ' .
|
||
'It contains many new features and optimizations including ' .
|
||
'named arguments, union types, attributes, constructor property promotion, ' .
|
||
'match expression, nullsafe operator, JIT, and ' .
|
||
'improvements in the type system, error handling, and consistency.');
|
||
|
||
?>
|
||
<section class="php8-section php8-section_dark php8-section_header center">
|
||
<div class="page-tools">
|
||
<div class="change-language">
|
||
<?php releases\php80\language_chooser('en'); ?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-section__content">
|
||
<div class="php8-logo">
|
||
<img src="/images/php8/logo_php8.svg" alt="php8" height="126" width="343">
|
||
</div>
|
||
<div class="php8-title">Released!</div>
|
||
<div class="php8-subtitle">
|
||
PHP 8.0 is a major update of the PHP language.<br class="display-none-md"> It contains many new features and
|
||
optimizations including named arguments, union types, attributes, constructor property promotion, match
|
||
expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency.
|
||
</div>
|
||
<div class="php8-button-wrapper center">
|
||
<a class="php8-button php8-button_light" href="/downloads">Upgrade to PHP 8 now!</a>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="php8-section center">
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="named-arguments">
|
||
Named arguments
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/named_params">RFC</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, \'UTF-8\', false);',
|
||
);?>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'htmlspecialchars($string, double_encode: false);',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<ul>
|
||
<li>Specify only required parameters, skipping optional ones.</li>
|
||
<li>Arguments are order-independent and self-documented.</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="attributes">
|
||
Attributes
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/attributes_v2">RFC</a> <a class="php8-rfc" href="/manual/en/language.attributes.php">Doc</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'class PostsController
|
||
{
|
||
/**
|
||
* @Route("/api/posts/{id}", methods={"GET"})
|
||
*/
|
||
public function get($id) { /* ... */ }
|
||
}',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'class PostsController
|
||
{
|
||
#[Route("/api/posts/{id}", methods: ["GET"])]
|
||
public function get($id) { /* ... */ }
|
||
}',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>Instead of PHPDoc annotations, you can now use structured metadata with PHP's native syntax.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="constructor-property-promotion">
|
||
Constructor property promotion
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/constructor_promotion">RFC</a> <a class="php8-rfc" href="/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion">Doc</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'class Point {
|
||
public float $x;
|
||
public float $y;
|
||
public float $z;
|
||
|
||
public function __construct(
|
||
float $x = 0.0,
|
||
float $y = 0.0,
|
||
float $z = 0.0
|
||
) {
|
||
$this->x = $x;
|
||
$this->y = $y;
|
||
$this->z = $z;
|
||
}
|
||
}',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'class Point {
|
||
public function __construct(
|
||
public float $x = 0.0,
|
||
public float $y = 0.0,
|
||
public float $z = 0.0,
|
||
) {}
|
||
}',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>Less boilerplate code to define and initialize properties.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="union-types">
|
||
Union types
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/union_types_v2">RFC</a> <a class="php8-rfc" href="/manual/en/language.types.declarations.php#language.types.declarations.union">Doc</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'class Number {
|
||
/** @var int|float */
|
||
private $number;
|
||
|
||
/**
|
||
* @param float|int $number
|
||
*/
|
||
public function __construct($number) {
|
||
$this->number = $number;
|
||
}
|
||
}
|
||
|
||
new Number(\'NaN\'); // Ok',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'class Number {
|
||
public function __construct(
|
||
private int|float $number
|
||
) {}
|
||
}
|
||
|
||
new Number(\'NaN\'); // TypeError',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are
|
||
validated at runtime.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="match-expression">
|
||
Match expression
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/match_expression_v2">RFC</a> <a class="php8-rfc" href="/manual/en/control-structures.match.php">Doc</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'switch (8.0) {
|
||
case \'8.0\':
|
||
$result = "Oh no!";
|
||
break;
|
||
case 8.0:
|
||
$result = "This is what I expected";
|
||
break;
|
||
}
|
||
echo $result;
|
||
//> Oh no!',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'echo match (8.0) {
|
||
\'8.0\' => "Oh no!",
|
||
8.0 => "This is what I expected",
|
||
};
|
||
//> This is what I expected',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>The new match is similar to switch and has the following features:</p>
|
||
<ul>
|
||
<li>Match is an expression, meaning its result can be stored in a variable or returned.</li>
|
||
<li>Match branches only support single-line expressions and do not need a break; statement.</li>
|
||
<li>Match does strict comparisons.</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="nullsafe-operator">
|
||
Nullsafe operator
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/nullsafe_operator">RFC</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'$country = null;
|
||
|
||
if ($session !== null) {
|
||
$user = $session->user;
|
||
|
||
if ($user !== null) {
|
||
$address = $user->getAddress();
|
||
|
||
if ($address !== null) {
|
||
$country = $address->country;
|
||
}
|
||
}
|
||
}',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'$country = $session?->user?->getAddress()?->country;',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator. When the
|
||
evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain
|
||
evaluates to null.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="saner-string-to-number-comparisons">
|
||
Saner string to number comparisons
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/string_to_number_comparison">RFC</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'0 == \'foobar\' // true',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'0 == \'foobar\' // false',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a
|
||
string and uses a string comparison.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="php8-compare">
|
||
<h2 class="php8-h2" id="consistent-type-errors-for-internal-functions">
|
||
Consistent type errors for internal functions
|
||
<a class="php8-rfc" href="https://wiki.php.net/rfc/consistent_type_errors">RFC</a>
|
||
</h2>
|
||
<div class="php8-compare__main">
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label">PHP 7</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'strlen([]); // Warning: strlen() expects parameter 1 to be string, array given
|
||
|
||
array_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__arrow"></div>
|
||
<div class="php8-compare__block example-contents">
|
||
<div class="php8-compare__label php8-compare__label_new">PHP 8</div>
|
||
<div class="php8-code phpcode">
|
||
<?php highlight_php_trimmed(
|
||
'strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array given
|
||
|
||
array_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0',
|
||
);?>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="php8-compare__content">
|
||
<p>Most of the internal functions now throw an Error exception if the validation of the parameters fails.</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="php8-section php8-section_light">
|
||
<h2 class="php8-h2">Just-In-Time compilation</h2>
|
||
<p>
|
||
PHP 8 introduces two JIT compilation engines. Tracing JIT, the most promising of the two, shows about 3 times better
|
||
performance on synthetic benchmarks and 1.5–2 times improvement on some specific long-running applications. Typical
|
||
application performance is on par with PHP 7.4.
|
||
</p>
|
||
<h3 class="php8-h3">
|
||
Relative JIT contribution to PHP 8 performance
|
||
</h3>
|
||
<p>
|
||
<img src="/images/php8/scheme.svg" width="900" alt="Just-In-Time compilation">
|
||
</p>
|
||
|
||
<div class="php8-columns">
|
||
<div class="php8-column">
|
||
<h2 class="php8-h2 php8-h2_margin-top">Type system and error handling improvements</h2>
|
||
<ul>
|
||
<li>
|
||
Stricter type checks for arithmetic/bitwise operators
|
||
<a href="https://wiki.php.net/rfc/arithmetic_operator_type_checks">RFC</a>
|
||
</li>
|
||
<li>
|
||
Abstract trait method validation <a href="https://wiki.php.net/rfc/abstract_trait_method_validation">RFC</a>
|
||
</li>
|
||
<li>
|
||
Correct signatures of magic methods <a href="https://wiki.php.net/rfc/magic-methods-signature">RFC</a>
|
||
</li>
|
||
<li>
|
||
Reclassified engine warnings <a href="https://wiki.php.net/rfc/engine_warnings">RFC</a>
|
||
</li>
|
||
<li>
|
||
Fatal error for incompatible method signatures <a href="https://wiki.php.net/rfc/lsp_errors">RFC</a>
|
||
</li>
|
||
<li>
|
||
The @ operator no longer silences fatal errors.
|
||
</li>
|
||
<li>
|
||
Inheritance with private methods <a href="https://wiki.php.net/rfc/inheritance_private_methods">RFC</a>
|
||
</li>
|
||
<li>
|
||
Mixed type <a href="https://wiki.php.net/rfc/mixed_type_v2">RFC</a>
|
||
</li>
|
||
<li>
|
||
Static return type <a href="https://wiki.php.net/rfc/static_return_type">RFC</a>
|
||
</li>
|
||
<li>
|
||
Types for internal functions
|
||
<a href="https://externals.io/message/106522">Email thread</a>
|
||
</li>
|
||
<li>
|
||
Opaque objects instead of resources for
|
||
<a href="https://php.watch/versions/8.0/resource-CurlHandle">Curl</a>,
|
||
<a href="https://php.watch/versions/8.0/gdimage">Gd</a>,
|
||
<a href="https://php.watch/versions/8.0/sockets-sockets-addressinfo">Sockets</a>,
|
||
<a href="https://php.watch/versions/8.0/OpenSSL-resource">OpenSSL</a>,
|
||
<a href="https://php.watch/versions/8.0/xmlwriter-resource">XMLWriter</a>, and
|
||
<a href="https://php.watch/versions/8.0/xmlwriter-resource">XML</a>
|
||
extensions
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<div class="php8-column">
|
||
<h2 class="php8-h2 php8-h2_margin-top">Other syntax tweaks and improvements</h2>
|
||
<ul>
|
||
<li>
|
||
Allow a trailing comma in parameter lists <a href="https://wiki.php.net/rfc/trailing_comma_in_parameter_list">RFC</a>
|
||
and closure use lists <a href="https://wiki.php.net/rfc/trailing_comma_in_closure_use_list">RFC</a>
|
||
</li>
|
||
<li>
|
||
Non-capturing catches <a href="https://wiki.php.net/rfc/non-capturing_catches">RFC</a>
|
||
</li>
|
||
<li>
|
||
Variable Syntax Tweaks <a href="https://wiki.php.net/rfc/variable_syntax_tweaks">RFC</a>
|
||
</li>
|
||
<li>
|
||
Treat namespaced names as single token <a href="https://wiki.php.net/rfc/namespaced_names_as_token">RFC</a>
|
||
</li>
|
||
<li>
|
||
Throw is now an expression <a href="https://wiki.php.net/rfc/throw_expression">RFC</a>
|
||
</li>
|
||
<li>
|
||
Allow ::class on objects <a href="https://wiki.php.net/rfc/class_name_literal_on_object">RFC</a>
|
||
</li>
|
||
</ul>
|
||
|
||
<h2 class="php8-h2 php8-h2_margin-top">New Classes, Interfaces, and Functions</h2>
|
||
<ul>
|
||
<li>
|
||
<a href="https://wiki.php.net/rfc/weak_maps">Weak Map</a> class
|
||
</li>
|
||
<li>
|
||
<a href="https://wiki.php.net/rfc/stringable">Stringable</a> interface
|
||
</li>
|
||
<li>
|
||
<a href="https://wiki.php.net/rfc/str_contains">str_contains()</a>,
|
||
<a href="https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions">str_starts_with()</a>,
|
||
<a href="https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions">str_ends_with()</a>
|
||
</li>
|
||
<li>
|
||
<a href="https://github.com/php/php-src/pull/4769">fdiv()</a>
|
||
</li>
|
||
<li>
|
||
<a href="https://wiki.php.net/rfc/get_debug_type">get_debug_type()</a>
|
||
</li>
|
||
<li>
|
||
<a href="https://github.com/php/php-src/pull/5427">get_resource_id()</a>
|
||
</li>
|
||
<li>
|
||
<a href="https://wiki.php.net/rfc/token_as_object">token_get_all()</a> object implementation
|
||
</li>
|
||
<li>
|
||
<a href="https://wiki.php.net/rfc/dom_living_standard_api">New DOM Traversal and Manipulation APIs</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="php8-section php8-section_dark php8-section_footer php8-footer">
|
||
<div class="php8-section__content">
|
||
<h2 class="php8-h2 center">
|
||
Better performance, better syntax, improved type safety.
|
||
</h2>
|
||
<div class="php8-button-wrapper center">
|
||
<a class="php8-button php8-button_light" href="/downloads">Upgrade to PHP 8 now!</a>
|
||
</div>
|
||
<div class="php8-footer__content">
|
||
<p>
|
||
For source downloads of PHP 8 please visit the <a href="http://www.php.net/downloads">downloads</a> page.
|
||
Windows binaries can be found on the <a href="http://windows.php.net/download">PHP for Windows</a> site.
|
||
The list of changes is recorded in the <a href="http://www.php.net/ChangeLog-8.php">ChangeLog</a>.
|
||
</p>
|
||
<p>
|
||
The <a href="/manual/en/migration80.php">migration guide</a> is available in the PHP Manual. Please
|
||
consult it for a detailed list of new features and backward-incompatible changes.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
|
||
|
||
|
||
<?php site_footer();
|