PHP 7.4 programming language release

After a year of development submitted programming language release PHP 7.4. The new branch includes a series of new features as well as several interoperability changes.

Key improvements in PHP 7.4:

  • Typed Properties - class properties can now include type declarations, for example:

    class User {
    public int $id;
    public string $name;
    }

  • Abbreviated syntax for defining functions "fn(parameter_list) => expr" with binding scope by value. For example "fn($x) => $x + $y" is analogous to "$fn2 = function ($x) use ($y) {return $x + $y;}");
  • Shorthand assignment operator "??=", which can be used to define a default value ("a ??= b" is similar to "a = a ?? b", if "a" is defined, its value is stored, and if not defined, its value is assigned to "b" );
  • Limited opportunity preservation of the inheritance hierarchy of types in their derived return types, or the ability to convert the hierarchy of source types to the opposite in the derived types of arguments (covariance return type and argument type contravariance). In PHP, you can now use the following constructs:

    class A {}
    class B extends A {}

    class Producer {
    public function method(): A {}
    }
    class ChildProducer extends Producer {
    public function method(): B {}
    }

  • Unpacking operator inside arrays "...$var", allowing perform substitution of existing arrays when defining a new array;

    $parts = ['apple', 'pear'];
    $fruits = ['banana', 'orange', …$parts, 'watermelon'];
    // ['banana', 'orange', 'apple', 'pear', 'watermelon'];

  • Possibility visual design of large numbers with separators in numeric literals (1_000_000_00);
  • Support weak links, which allow you to keep a reference to the object, but do not block the removal of the associated object by the garbage collector;
  • New mechanism object serialization (a combination of Serializable and __sleep()/__wakeup()), which replaced the Serializable interface, which will be deprecated;

    // Returns an array containing all object states;
    public function __serialize(): array;

    // Restores the state of the object from the array
    public function __unserialize(array $data): void;

  • Allow throwing exceptions from a method __toString();
  • Added support for prefetching the object code cache. Parameter added for setting
    opcache. preload, through which you can specify the PHP script that will be compiled and run when the server starts. This script can load the opcode of other files by including them directly or by using the opcache_compile_file() function;

  • The crc32c function has been added to the Hash extension to calculate checksums using the Castagnoli polynomial;
  • Added support for password hashing methods argon2i and argin2id to the password_hash() function, in the implementation of the Sodium library, if PHP is built without libargon;
  • Added function mb_str_split(), similar to str_split(), but operating not by bytes, but by character positions in a multibyte string;
  • Added the ability to pass an array with tag names to the strip_tags() function, i.e. instead of strip_tags($str, ' ') now you can specify strip_tags($str, ['a', 'p']);
  • proc_open() allows enumeration of operands in an array instead of specifying a string to run, as well as the ability to redirect streams and support for a null file descriptor;

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

    // Like 2>&1 in the shell
    proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);

    // like 2>/dev/null or 2>nul in shell
    proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['null']], $pipes);

  • Firebird/Interbase, Recode and WDDX extensions are excluded from the base distribution. These extensions are now distributed via PECL;
  • Moved to rank obsolete features such as nested ternary operators without parentheses, accessing array elements and strings using curly braces ("$var{$idx}"), the is_real() function and casting to real, using the parent keyword without a parent class, parameter allow_url_include configurations, using array_key_exists() on objects.

    Deprecated functions get_magic_quotes_gpc(), get_magic_quotes_runtime(),
    hebrevc(), convert_cyr_string(), money_format(), ezmlm_hash(), restore_include_path(), ldap_control_paged_result_response(), ldap_control_paged_result(), ReflectionType::__toString().

    Provided a warning about using a deprecated feature when trying to process invalid characters in functions
    base_convert(), bindec(), octdec() and hexdec(), and when specifying a non-string pattern in mb_ereg_replace().

Source: opennet.ru

Add a comment