You are on page 1of 7

Downloads

Documentation
Get Involved
Help

Search

Getting Started
Introduction
A simple tutorial
Language Reference
Basic syntax
Types
Variables
Constants
Expressions
Operators
Control Structures
Functions
Classes and Objects
Namespaces
Errors
Exceptions
Generators
References Explained
Predefined Variables
Predefined Exceptions
Predefined Interfaces and Classes
Context options and parameters
Supported Protocols and Wrappers

Security
Introduction
General considerations
Installed as CGI binary
Installed as an Apache module
Session Security
Filesystem Security
Database Security
Error Reporting
Using Register Globals
User Submitted Data
Magic Quotes
Hiding PHP
Keeping Current
Features
HTTP authentication with PHP
Cookies
Sessions
Dealing with XForms
Handling file uploads
Using remote files
Connection handling
Persistent Database Connections
Safe Mode
Command line usage /
Garbage Collection
DTrace Dynamic Tracing

Function Reference
Affecting PHP's Behaviour
Audio Formats Manipulation
Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Credit Card Processing
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions

Keyboard Shortcuts
?
This help
j
Next menu item
k
Previous menu item
gp
Previous man page
gn
Next man page
G
Scroll to bottom
gg
Scroll to top
gh
Goto homepage
gs
Goto search
(current page)
/
Focus search box

New Classes and Interfaces »


« Migrating from PHP 7.3.x to PHP 7.4.x

PHP Manual
Appendices
/
Migrating from PHP 7.3.x to PHP 7.4.x

Change language: English


Edit Report a Bug

New Features ¶
PHP Core ¶

Typed properties ¶

Class properties now support type declarations.

<?php
class User {
    public int $id;
    public string $name;
}
?>
The above example will enforce that $user->id can only be assigned integer values and $user->name can only be
assigned string values.

Arrow functions ¶

Arrow functions provide a shorthand syntax for defining functions with implicit by-value scope binding.

<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
?>

Limited return type covariance and argument type contravariance ¶

The following code will now work:

<?php
class A {}
class B extends A {}

class Producer {
    public function method(): A {}
}
class ChildProducer extends Producer {
    public function method(): B {}
}
?>
Full variance support is only available if autoloading is used. Inside a single file only non-cyclic type references are
possible, because all classes need to be available before they are referenced.

Null coalescing assignment operator ¶

<?php
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault(); /
}
?>

Unpacking inside arrays ¶

<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
?>

Numeric literal separator ¶

Numeric literals can contain underscores between digits.

<?php
6.674_083e-11; // float
299_792_458;   // decimal
0xCAFE_F00D;   // hexadecimal
0b0101_1111;   // binary
?>

Weak references ¶

Weak references allow the programmer to retain a reference to an object that does not prevent the object from being
destroyed.

Allow exceptions from __toString() ¶

Throwing exceptions from __toString() is now permitted. Previously this resulted in a fatal error. Existing recoverable
fatal errors in string conversions have been converted to Error exceptions.

CURL ¶

CURLFile now supports stream wrappers in addition to plain file names, if the extension has been built against libcurl
>= 7.56.0.

Filter ¶

The FILTER_VALIDATE_FLOAT filter now supports the min_range and max_range options, with the same semantics as
FILTER_VALIDATE_INT.

FFI ¶
FFI is a new extension, which provides a simple way to call native functions, access native variables, and create/access
data structures defined in C libraries.

GD ¶

Added the IMG_FILTER_SCATTER image filter to apply a scatter filter to images.

Hash ¶
Added crc32c hash using Castagnoli's polynomial. This CRC32 variant is used by storage systems, such as iSCSI, SCTP,
Btrfs and ext4.

Multibyte String ¶ /
Added the mb_str_split() function, which provides the same functionality as str_split(), but operating on code points
rather than bytes.

OPcache ¶
Support for preloading code has been added.

Regular Expressions (Perl-Compatible) ¶

The preg_replace_callback() and preg_replace_callback_array() functions now accept an additional flags argument,
with support for the PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags. This influences the format of the
matches array passed to to the callback function.

PDO ¶
The username and password can now be specified as part of the PDO DSN for the mysql, mssql, sybase, dblib, firebird
and oci drivers. Previously this was only supported by the pgsql driver. If a username/password is specified both in the
constructor and the DSN, the constructor takes precedence.

It is now possible to escape question marks in SQL queries to avoid them being interpreted as parameter placeholders.
Writing ?? allows sending a single question mark to the database and e.g. use the PostgreSQL JSON key exists (?)
operator.

PDO_OCI ¶

PDOStatement::getColumnMeta() is now available.

PDO_SQLite ¶

PDOStatement::getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT) allows checking whether the statement


is read-only, i.e. if it doesn't modify the database.

PDO::setAttribute(PDO::SQLITE_ATTR_EXTENDED_RESULT_CODES, true) enables the use of SQLite3 extended


result codes in PDO::errorInfo() and PDOStatement::errorInfo().

SQLite3 ¶

Added SQLite3::lastExtendedErrorCode() to fetch the last extended result code.

Added SQLite3::enableExtendedResultCodes($enable = true), which will make SQLite3::lastErrorCode() return


extended result codes.

Standard ¶

strip_tags() with array of tag names ¶

strip_tags() now also accepts an array of allowed tags: instead of strip_tags($str, '<a><p>') you can now write
strip_tags($str, ['a', 'p']).

Custom object serialization ¶

A new mechanism for custom object serialization has been added, which uses two new magic methods: __serialize and
__unserialize.

<?php
// Returns array containing all the necessary state of the object.
public function __serialize(): array;
/
// Restores the object state from the given data array.
public function __unserialize(array $data): void;
?>
The new serialization mechanism supersedes the Serializable interface, which will be deprecated in the future.

Array merge functions without arguments ¶

array_merge() and array_merge_recursive() may now be called without any arguments, in which case they will return an
empty array. This is useful in conjunction with the spread operator, e.g. array_merge(...$arrays).

proc_open() function ¶

proc_open() now accepts an array instead of a string for the command. In this case the process will be opened directly
(without going through a shell) and PHP will take care of any necessary argument escaping.

<?php
proc_open(['php', '-r', 'echo "Hello World\n";'], $descriptors, $pipes);
?>

proc_open() now supports redirect and null descriptors.

<?php
// Like 2>&1 on the shell
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
// Like 2>/dev/null or 2>nul on the shell
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['null']], $pipes);
?>

argon2i(d) without libargon ¶

password_hash() now has the argon2i and argon2id implementations from the sodium extension when PHP is built
without libargon.

add a note

User Contributed Notes 1 note

up
down
0
Rain ¶
9 hours ago
It should be noted that typed properties internally are never initialized to a default null. Unless
of course you initialize them to null yourself. That's why you will always going to encounter this
error if you try to access them before initialization.

**Typed property foo::$bar must not be accessed before initialization**

<?php
class User
{
    public $id;
    public string $name; // Typed property (Uninitialized)
    public ?string $age = null; //  Typed property (Initialized)
}

$user = new User;


var_dump(is_null($user->id)); // bool(true) /
var_dump(is_null($user->name)); // PHP Fatal error: Typed property User::$name must not be accessed
before initialization
var_dump(is_null($user->age));// bool(true)
?>

Another thing worth noting is that it's not possible to initialize a property of type object to
anything other than null.  Since the evaluation of properties happens at compile-time and object
instantiation happens at runtime. One last thing, callable type is not supported due to its
context-dependent behavior.
add a note

Migrating from PHP 7.3.x to PHP 7.4.x


New Features
New Classes and Interfaces
New Functions
New Global Constants
Backward Incompatible Changes
Deprecated Features
Removed Extensions
Other Changes
Windows Support

Copyright © 2001-2020 The PHP Group


My PHP.net
Contact
Other PHP.net sites
Privacy policy

You might also like