HTML_ENTITY_DECODE

      22
PHP 8.1 is the new PHP version released November 2021. This version comes with new features, performance improvements, & changes that will mở cửa more opportunities for PHP developers lớn work efficiently and more creatively.

Bạn đang xem: Html_entity_decode

There are over 20 new features, tons of changes, & several deprecations in PHP 8.1. This article will go through them và help you smoothly transition from PHP 8.0 khổng lồ PHP 8.1.

*
*

New Features và Upgrades in PHP 8.1

This new major PHP version introduces over đôi mươi new features. Keep in mind that there may be changes after the release date that can make this danh sách longer or shorter.

Here are some of the new features in PHP 8.1.

Enumerations

PHP 8.1 supports enumerations, or enum for short. It’s an enumerated data type that consists of a fixed mix of possible values.

The syntax uses enum to lớn declare enumerations, followed by its user-defined name. Then, it’s followed by case to declare the possible values. Enumerations can have any number of cases, from zero khổng lồ as many as you need.

The enumeration RFC uses standard playing cards to explain how it works. There are four fixed suits – Spades, Hearts, Diamonds, & Clubs. You can enumerate these suits with enum in PHP:

enum Suit case Spades;case Hearts;case Diamonds;case Clubs;Now, the Suit enum has four possible values of thẻ suits. You can use a function & enforce types when accepting or returning a suit value with a syntax, for example, Suit::Clubs.

function pick_card(Suit $suit) pick_card(Suit::Spades);Enumerations can also contain an optional string or int value. This is called backed Enum, and it has the following criteria:

Declare the scalar type, whether string or int, in the Enum declaration.All cases have values.All cases contain the same scalar type, whether string or int.Each case has a chất lượng value.

Suppose we want to assign a score khổng lồ the suits in the previous example. We can use this syntax:

enum Suit: string {case Spades = ‘10’;case Hearts = ‘8’;case Diamonds = ‘5’;case Clubs = ‘3’;

Fibers

This new feature introduces concurrent execution in PHP programming. However, concurrent execution in PHP doesn’t mean that the actions are executed at the same time.

For example, there’s a point where the main code will start a fiber. The code within the fiber will be executed separately in a different thread.

Also, the main program can’t suspend or terminate fibers. Only the code within the fiber can suspend itself and return any data khổng lồ the main program. The main program, then, can continue the Fiber execution from the point of suspension.

A complete stack of a PHP program with fibers can be complicated, but here’s a simple example of fiber usage in an Echo program:

$fiber = new Fiber(function(): void echo "Hello from the Fiber "; Fiber::suspend(); echo "Welcome back lớn the Fiber ";);echo "Starting the program ";$fiber->start();echo "Taken control back ";echo "Resuming Fiber ";$fiber->resume();echo "Program ends ";Fiber::suspend() is where the fiber will suspend its execution. Then, when the main code calls for the fiber again with $fiber->resume(), the fiber will start from the point of suspension. Hence, it will echo Welcome back khổng lồ the Fiber.

Here’s what the output đầu ra should look like:

Starting the programHello from the FiberTaken control backResuming FiberWelcome back khổng lồ the FiberProgram ends

Pure Intersection Type

PHP 8.1 supports intersection types that allow declaring multiple class types for a value. At a glance, this sounds similar lớn union types that were introduced in PHP 8.0. However, there’s a significant difference.

Union types allow the value to fulfill any of the declared types. For example, if strings và integers are declared, the value can be either strings or declared.

Pure intersection types, however, only allow values that fulfill all of the declared types. This is why the pure intersection types can only use class or interface types, as most standard types lượt thích strings & integers can’t be fulfilled simultaneously.

To declare a pure intersection type in PHP 8.1, use & (AND) operator. Here’s an example:

function count_and_iterate(Iterator&Countable $value) foreach($value as $val) count($value);The $value must be an object that implements the Iterator and Countable interfaces. Otherwise, it will cause a type error.

never Return Type

There’s a new never return type in PHP 8.1. When a never return type is declared in a function, it won’t return a value. The function must also kết thúc with an exit statement explicitly or implicitly.

Here’s an example of a simple function with the never return type:

function redirect(string $url): never header("Location: " . $url); exit();When you use a never return type, the rest of the code in the program will not be executed or return any value. In practical use, the never return type is useful lớn indicate that the function won’t execute the rest of the code and will terminate or throw.

In a way, the never return type is similar khổng lồ the void return type. However, the void return type continues the execution after the function.

array_is_list Type

The new array_is_list function in PHP 8.1 helps you see whether an array contains entries that are a list. It will check the array keys – if they are in sequential order, start from zero, have no gaps, & all integers, it will return true. It will also return true by default for empty arrays.

For example, the following array_is_list function will return true:

array_is_list(<>);array_is_list(<1, 2, 3>);array_is_list(<‘rock’, 2, 3>);array_is_list(<‘rock’, scissor>);array_is_list(<0 => "rock", "scissor">);array_is_list(<0 => "rock", 1 => "scissor">);Conversely, here are examples of false array_is_list functions:

array_is_list(<1 => "rock", "scissor">); // false, doesn’t start with 0array_is_list(<1 => "rock", 0 => "scissor">); // false, not in orderarray_is_list(<0 => "rock", "suit" => "paper">); false, non-integer keysarray_is_list(<0 => "rock", 2 => "paper">); false, non-sequential

# Attribute

This new attribute indicates a mismatching tentative return type shouldn’t result in a deprecation notice. Before PHP 8.0, attribute syntax was parsed as a code phản hồi so that it wouldn’t cause a syntax error.

The new attribute addition # to lớn a class method in the PHP 8.1 version won’t cause any problem – it’ll only prevent the deprecation notice.

This is an example of # in a PHP code:

class Sample implements ArrayAccess # public function offsetGet(mixed $offset) // ...Note that this attribute won’t have any effect anymore when the tentative return types are upgraded lớn standard return types.

readonly Properties

readonly is a new class property in PHP 8.1 & can only be initialized once from the class where it’s declared. Any class with this property will be unmodifiable and will result in an error if you force it khổng lồ change the value.

Here’s an example of how you declare a readonly property in a class:

class author public readonly int $authid; public function __construct(int $authid) $this->authid = $authid; $author = new User(25);Now, since the readonly has already been initialized, you can’t modify the $authid in the code.

If you try to input a line like this:

$author->authid = 15It will result in this error:

Error: Cannot modify readonly property User::$uid in ...:..

fsync() and fdatasync() Functions

fsync() và fdatasync() are new additions khổng lồ the file system functions in PHP 8.1. They have similarities to fflush(), which is used for flushing buffers khổng lồ the operating system. However, fsync() & fdatasync() flush the buffer to the physical storage.

The key difference between them is that the fsync()function includes metadata when synchronizing files’ changes, while the fdatasync() doesn’t.

Here’s an example of the fsync() function in a code:

$file = "sample.txt";$test = fopen($file, "h");fwrite($test, "sample data");fwrite($test, " ");fwrite($test, "another data");fsync($test);fclose($test);You can use either fsync()or fdatasync()in line 6. When the function is called, the operating system is requested to flush the data to lớn the storage.

These functions are useful when the program needs consistent data storage. Plus, it makes it possible khổng lồ retrieve the data in case the application crashes or the system fails.

Sodium XChaCha20 Functions

Sodium is a PHP extension that contains a cryptographic library. It supports the XChaCha20 encryption algorithm, a stream cipher that’s a variant of ChaCha20.

In PHP 8.1, three new functions let you encrypt và decrypt data using XChaCha20 without authentication. This method is called detached mode, và the functions are as follows:

sodium_crypto_stream_xchacha20_keygen – generate a random key to use with the sodium_crypto_stream_xchacha20 function.sodium_crypto_stream_xchacha20 – generate a keystream of pseudorandom bytes by expanding the cryptographic nonce và the key generated earlier.sodium_crypto_stream_xchacha20_xor – encrypt a message using a nonce và secure key without authentication.

Alongside the new functions, two new PHP constants are defined in the global namespace:

SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBITES, assigned 32.SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES, assigned 24.

new in Initializers

PHP 8.1 now allows the use of new expressions to initialize a value inside a function declaration.

When using PHP 8.0, we have to initialize the value for an optional parameter outside the constructor. While this works just fine, it makes the code a little bit longer. Here’s an example from the new RFC:

class chạy thử private Logger $logger; public function __construct( ?Logger $logger = null, ) $this->logger = $logger ?? new NullLogger; In PHP 8.1, we can simply write it lượt thích this:

class chạy thử public function __construct( private Logger $logger = new NullLogger, )

String-Keyed Array Unpacking Support

The previous PHP version added support for the array spread operator (…). It works for array unpacking và as an alternative to the array_merge() function.

However, the spread operator used to support numeric keys only. Using the operator or function for string-keyed arrays would result in an error.

PHP 8.1 now supports array unpacking for string-keyed arrays. Suppose you have these arrays:

$array_1 = <"a", "b">;$array_2 = <"c", "d">;You can unpack the array’s keys using the spread operator.

$array = <...$array1, ...$array2>;var_dump($array);//<"a", "b", "c", "d">;Or, unpack the array’s keys using the array_merge() function:

$array = array_merge($array1, $array2);var_dump($array);//<"a", "b", "c", "d">;

First-class Callable Syntax

PHP 8.1 introduces a new first-class callable syntax to lớn create a callable from inside the current scope.

Prior khổng lồ PHP 8.1, syntax for a callable uses Closure::fromCallable, such as this:

$callable = Closure:: fromCallable(‘thietkewebhcm.com.vn’);With the new syntax, you can use (...) after the object instead of using Closure::from Callable. This makes it easier lớn create a first-class callable. Here’s an example of the above function using the new syntax:

$callable = thietkewebhcm.com.vn(...);

New IntlDatePatternGenerator Class

In the previous version of PHP, you can create a localized date & time only with the pre-defined IntlDateFormatter. There are eight pre-defined formats – four basic formats và four relative variants that use yesterday, today, and tomorrow.

While these are reasonable options, it’s not as customizable as it is in PHP 8.1.

The new IntlDatePatternGenerator class allows you to specify the format for a date, month, & time. The exact order itself can be left khổng lồ the formatter.

Here’s an example of its use from the IntlDatePatternGenerator RFC:

final Class Constants

Declaring the new final flag on class constants prevents them from being overridden or extended by sub-classes.

If you try lớn expand or override a final class, it will result in a fatal error. Here’s an example:

class First final public const thử nghiệm = "1";class Second extends First public const chạy thử = "2";It will result in:

Fatal error: Second::TEST cannot override final constant First::TEST in %s on line %dThis flag can’t be used for a private constant as it can’t be accessed outside the class. Declaring both final and private constants together will result in a fatal error.

class First { final private const kiểm tra = "1";Fatal error: Private constant First::TEST cannot be final as it is never overridden in ... On line ...

Explicit Octal Numeral Notation

PHP supports several numeral systems, such as default decimal (base-10), binary (base-2), octal (base-8), and hex (base-16). However, numeral systems other than decimal have khổng lồ use the following prefixes:

Hex – 0xBinary – 0bOctal – 0

PHP 8.1 introduces o (lowercase) and O (uppercase) prefixes for the octal numerals. This means that octal numerals are now more apparent and readable.

Have a look at these examples:

echo 77; // 77, numeric stringecho 077; // 63, old-format octal numeralecho 0o77; // 63, octal numeral in PHP 8.1Note that this new prefix is just an addition. The existing 0 prefix will work the same way in PHP 8.1.

$_Files : full_path Value for Directory Uploads

$_FILES is a super global variable containing names, sizes, & MIME types of files uploaded.

In the earlier version of PHP 8.1, $_FILES didn’t store the relative paths or the exact directory lớn the server. Therefore, you couldn’t upload an entire directory with an HTML file upload form.

The new full_path key solves the problem. It stores the relative paths and reconstructs the exact directory structure on the server, making directory uploads possible.

var_dump($_FILES);array(1) <"myupload">=> array(6) <"name">=> array(2) <0>=> string(8) "file.txt" <1>=> string(8) "file.txt" <"full_path">=> array(2) <0>=> string(19) "foo/test1/file.txt" <1>=> string(19) "foo/test2/file.txt" <"tmp_name">=> array(2) <0>=> string(14) "/tmp/phpV1J3EM" <1>=> string(14) "/tmp/phpzBmAkT" // ... + error, type, kích cỡ There are two different file.txt files in the example above, but they are stored in different directories. If you use the $_FILES<“myupload”><“name”> array, there will be duplicate entries, because both files are specified with the same name.

However, the $_FILES<“myupload”><“full_path”> array contains the path for each file. It prevents the duplication that happens on the previous array.

WebP Lossless Encoding and support for GdImage

In PHP 8.1, there’s a new lossless WebP encoding support for the GdImage object. It can be enabled using GD extension & libwebp version 0.2.0.

The new PHP constant khổng lồ declare the lossless WebP encoding is IMG_WEBP_LOSSLESS, along with imagewebp function. In a PHP code, it looks lượt thích this:

$image = imagecreatefromjpeg("image.jpg");imagewebp($image, "image.webp", IMG_WEBP_LOSSLESS);

AVIF Image Support

In addition lớn WebP support, PHP 8.1 – using the GD extension, also supports the AVIF image format. It’s a relatively new image format, based on the AV1 đoạn phim format. It provides higher compression and is royalty-free.

To địa chỉ cửa hàng AVIF image tư vấn to the PHP image processing, you must compile the GD extension with AVIF support. You’ll also need libavid version 0.8.2 or higher for the GD extension.

Depending on your operating system, run the following commands:

Ubuntu/Debian – apt install libavif-devRHEL/Fedora – dnf install libavif-devel

The next step is lớn run the --with-avif flag with the ./configure script khổng lồ compile PHP with the AVIF support.

./buildconf --force./configure --enable-gd --with-avifThen, run the following command to demo whether the AVIF support is now enabled:

php -i | grep AVIFIf it’s enabled, you should see this output:AVIF tư vấn => enabled

DNS-over-HTTPS Support

DNS-over-HTTPS increases privacy & security by running the DNS resolution through the HTTPS protocol. It encrypts data sent by the client computer khổng lồ the DNS resolver.

The Curl extension in PHP 8.1 now allows specifying a vps for the DNS-over-HTTPS protocol. Lớn use this new feature, you need lớn compile PHP with libcurl 7.62 or higher. That said, most operating systems already use Curl 7.68, so this shouldn’t be a problem.

To specify DNS-over-HTTPS, use the CURLOPT_DOH_URL option.

$ch = curl_init("https://website.com");curl_setopt($ch, CURLOPT_DOH_URL, "https://dns.google/dns-query");curl_exec($ch);Note that the vps URL has to lớn be an HTTPS URL, and it will be validated when the request is executed. The request will fail if the URL for the DNS vps is not valid or doesn’t provide a valid response.

We used Google’s public DNS server in the example above, but you can also pick any other public DNS-over-HTTPS server.

CurlStringFile Class for tệp tin Uploads from Strings

The Curl extension has a new CURLStringFile class that works similarly to lớn the CURLFile class for tệp tin uploads. However, the CURLFile class only accepts a URI or a file path, whereas CURLStringFile accepts the file’s nội dung instead of a path or a URI.

The new PHP 8.1 class makes it easier khổng lồ create a tệp tin upload request using data stored in the memory. For example, use this to lớn upload an image processed in PHP or stored in a PHP variable.

Here’s an example of using the CURLStringFile in a PHP code:

$txt = "test content";$txt_curlfile = new CURLStringFile($txt, "text/plain", "test.txt");$ch = curl_init("http://website.com/upload.php");curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, <"file" => $txt_curlfile>);curl_exec($ch);

Algorithm tư vấn for MurmurHash3 and xxHash Hashes

PHP 8.1 introduces tư vấn to two non-cryptographic hashing algorithms – MurmurHash3 and xxHash. Non-cryptographic hashes are faster than most of the current PHP hashing algorithms, yet still provide great output đầu ra dispersion & randomness.

MurmurHash3

For MurmurHash3, here are the variants supported:

murmur3a – 32-bit hashmurmur3c – 128-bit hash on x86 architecturemurmur3f – 128-bit hash on x64 architecture

To hash a string using MurmurHash3, use syntax lượt thích this:

hash(‘murmur3a’, ‘hash_example’);hash(‘murmur3c’, ‘hash_example’);hash(‘murmur3f’, ‘hash_example’);You can update the values in sequence without hashing the đầu vào string as a whole. This is known as a streaming hash.

$context = hash_init(‘murmur3a’);hash_update($context, "hash");hash_update($context, "_");hash_update($context, ‘example");$hash = hash_final($context);xxHash

PHP 8.1 supports the following xxHash algorithm:

xxh32 – 32-bit hash outputxxh64 – 64-bit hash outputxxh3 – 64-bit hash outputxxh128 – 128-bit hash output

Here’s the syntax khổng lồ hash a string using xxHash:

hash(‘xxh32’, ‘hash_example’);hash(‘xxh64’, ‘hash_example’);hash(‘xxh3’, ‘hash_example’);hash(‘xxh128’, ‘hash_example’);Similar to lớn MurmurHash3, xxHash is also a streaming hash that allows you khổng lồ update the values in sequence.

$context = hash_init(‘xxh32’);hash_update($context, "hash");hash_update($context, "_");hash_update($context, ‘example");$hash = hash_final($context);

Performance Improvements

PHP 8.1 adds a new feature to lớn remove the overhead of class inheritance, the Inheritance Cache.

Previously, the opcache compiled và cached PHP classes separately. Then, it linked these classes at run-time during each request.

The linking process requires compatibility checks and borrowing methods, properties, or constants from parent classes and traits. This slows down the execution, although the result will be the same for each request.

The new Inheritance Cache solves this by linking all unique dependent classes và saving them in the opcache shared memory. This patch also removes limitations for immutable classes – all classes stored in the opcache are now immutable.

It results in fewer instructions for each request & an 8% improvement for the Symfony “Hello, World!” application.

MySQL_I_REFRESH_Replica Constant

MySQLi introduces a new constant for PHP 8.1 called MYSQLI_REFRESH_REPLICA. This constant has the same functionality as the existing MYSQLI_REFRESH_SLAVE constant.

The new name addresses racial insensitivity regarding the slave terminology. However, the existing MYSQLI_REFRESH_SLAVE constant won’t be removed or deprecated on PHP 8.1.

Changes in PHP 8.1

PHP 8.1 introduces some changes in syntax và functionalities. Most of them don’t significantly alter the existing features, but you should be aware of these changes if you continue using an existing PHP application on PHP 8.1.

HTML Encoding/Decoding Functions Convert Single Quotes và Substitute by Default

PHP 8.1 brings changes lớn functions that decode and encode HTML entities. HTML entity is a textual representation of a character in the HTML code. Common examples of HTML entities are the characters that hóa trang HTML tags such as or .

The browser will interpret these characters as HTML code if you use them in an HTML document. Therefore, if you want the browser lớn show them in their original form, you must use their HTML entities.

For example, you want the browser khổng lồ render as a text. Thus, you write it in the HTML document as represent respectively.

PHP has some functions to lớn convert HTML characters into HTML entities:

htmlspecialchars – converts special characters khổng lồ HTML entities.htmlspecialchars_decode – converts HTML entities to special characters.htmlentities – converts applicable characters khổng lồ HTML entities.html_entitity_decode – converts HTML entities to their related characters.get_html_translation_table – returns the translation table for the htmlspecialchars and htmlentities functions.

In the previous PHP versions, these functions didn’t convert single quotes () and would return an empty string. This changed in PHP 8.1 as they now convert single quote characters lớn its HTML entity, '.

The change is done by switching the ENT_COMPAT signature in the functions khổng lồ ENT_QUOTES | ENT_SUBSTITUTE. Here are the new syntax:

htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE);htmlspecialchars_decode($string, ENT_QUOTES | ENT_SUBSTITUTE);htmlentities($string, ENT_QUOTES | ENT_SUBSTITUTE);html_entity_decode($string, ENT_QUOTES | ENT_SUBSTITUTE);get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES | ENT_SUBSTITUTE);

Interactive Shell Requires Readline Extension

In the previous PHP versions, it’s possible to open the interactive shell with the php -a option. However, the interactive features were not enabled since they require the readline extension.

Xem thêm: Hướng Dẫn Trỏ Tên Miền Về Blogspot Chi Tiết Cho Người Mới, Hướng Dẫn Trỏ Tên Miền Về Blogspot

Therefore, the php -a option was meaningless and the same as opening a non-interactive shell using the php command.

This is now fixed in PHP 8.1. Using the php -a option in the command line without the readline extension enabled will return an error message.

php -aInteractive shell (-a) requires the readline extension.However, if you have the extension enabled, it will xuất hiện the interactive shell.

php -aInteractive shellphp >php > echo "Hello";Hellophp > function test() {php { echo "Hello";php php > test();Hello

MySQLi mặc định Error Mode Set to Exceptions

Before PHP 8.1, the default error handling behavior of MySQLi was to silence the errors. Developers had to set their error handling functions, or the code wouldn’t follow the strict error handling.

In PHP 8.1, the mặc định error handling is set to lớn throw an exception.

MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICTThis is a breaking change and, as the value is different, it will cause a compatibility issue. However, solve this by explicitly setting the error handling using the MYSQLI_REPORT function before making the first MySQLi connection.

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

Customizable Line Ending for CSV Writing Functions

In the previous PHP versions, the fputcsv() và SplFileObject::fputcsv functions are hard-coded lớn “ ” end-of-line character. PHP 8.1 introduces a new eol function to lớn allow customizable end-of-line characters.

However, this parameter is optional, & the function will still use “ ” by default. If you have any existing code or application that doesn’t explicitly phối an end-of-line character, it will still use “ ”.

If you want lớn use the eol parameter for a custom end-of-line character, here is a syntax example:

function fputcsv($stream, array $fields, string $separator = ",", string $enclosure = """, string $escape = "\", string $eol = " "): int|false public function fputcsv(array $fields, string $separator = ",", string $enclosure = """, string $escape = "\", string $eol = " ") Note that you have khổng lồ use double quotes with end-of-line characters such as , , or so they will be interpreted correctly.

Compact Function Calls Warning for Parameters With Non-string & Non-array String

The compact function is useful khổng lồ create an array that contains variables and their values. Here’s an example of a compact function:

$low_cost = ‘Shared hosting’;$mid_cost = ‘VPS hosting’;$high_cost = ‘Dedicated hosting’;compact (‘low_cost’, ‘mid_cost’, ‘high_cost’);// “Shared hosting”, ‘mid_cost’ => “VPS hosting”, ‘high_cost’ => “Dedicated hosting”>In its documentation, the compact function only accepts string parameters or arrays with string values. However, it might look lượt thích it accepts non-string & non-array parameters, but it actually ignores them.

If you declare strict-types with declare(strict_types=1), the function won’t perform a strict-type checking either.

This is changed in PHP 8.1, as the compact function now shows a warning if the argument contains a non-string or an array that doesn’t consist of string keys.

compact(null);// <>PHP Warning: compact(): Argument #1 must be string or array of strings, null given in ... On line …That said, declaring strict types won’t have an impact, and the function still won’t throw a type error.

Phar Signature Algorithm Changed From SHA1 lớn SHA256

The Phar extension is used khổng lồ pack a PHP application into an archive Phar file. This file can then be executed in any PHP environment.

Each Phar file contains a signature khổng lồ verify the file’s integrity before the execution. The mặc định signature algorithm for the Phar file used khổng lồ be SHA1. However, in PHP 8.1, that changes khổng lồ SHA256.

You can override the signature algorithm by using Phar::setSignatureAlgorithm and select MD5, SHA1, SHA256, or SHA512. Here’s a code snippet of that:

$filename = "test.phar";$phar = new Phar($filename);$phar->buildFromDirectory("src/");$phar->setDefaultStub("index.php", "/index.php");$phar->setSignatureAlgorithm(Phar::SHA512);Note that all PHP versions since 5.3 tư vấn the SHA256 algorithm. This means all Phar files generated with PHP 8.1 will still be compatible with older versions. However, two new signature algorithms – OpenSSL-256 and OpenSSL-512 – are incompatible with older PHP versions.

JsonSerializable Implementation in SplFixedArray

SplFixedArray creates a fixed number of elements in the array & the keys will be integers in the range.

In previous PHP versions, the json_encode function encodes SplFixedArray instance as objects instead of arrays. This will change as SplFixedArray implements JsonSerializable interface và encodes it as arrays.

$array = new SplFixedArray(3);$array<> = "Web hosting";$array<> = "Domain name";$array<> = "SSL certificate";echo json_encode($array);Prior PHP 8.1, the output would be as follows:

"0":"Web hosting","1":"Domain name","2":"SSL certificate"In PHP 8.1, it will be lượt thích this:

<"Web hosting","Domain name","SSL certificate">

Resources to lớn Object Migrations

PHP development is planning lớn move resources khổng lồ standard class objects.

One of the problems is that you can’t coerce or cast to resource type. Plus, the lack of a resource type prevents some resource-related functionalities from adding stricter types.

Another challenge is maintaining some resource types with features like internal state, memory handling, and reference counting. Therefore, in this case, PHP classes can be used instead.

Those are some of the reasons why the PHP team is migrating resource types to lớn objects. However, resource types have been used extensively in PHP applications, so the team needs to lớn ensure non-disruptive migration.

FTP Connection Resources Are Now FTPConnection Class Objects

In the previous PHP version, we used ftp_connect() or ftp_ssl_connect() functions khổng lồ create an FTP connection. These functions return a resource of type ftp.

In PHP 8.1, these functions return a FTPConnection class. This new class is also declared final, meaning that you can’t extend it. It also minimizes the potential backward-compatibility problems if the PHP vi xử lý core changes the methods of the FTPConnection class.

GD fonts Identifiers Are Now GdFont Class Objects

In previous PHP versions, the imageloadfont()function from the GD extension was used to return a font-identifier resource ID in an integer.

In the new PHP version, the resource ID has migrated to lớn a GdFont class.

To make the change less disruptive, all functions that accept a resource ID from imageloadfont() also accept the new GdFont class object.

file_info Resources Are Now finfo Objects

Some of the functions from the finfo extension return và accept resource objects with the file_info type. This is now changed to finfo class objects to lớn align with PHP’s migration plan.

Note that the finfo class has been available since PHP 5.3. However, the finfo_*()functions accept or return resources. This is now fixed, and it is even possible to change the return values lớn objects.

These are the functions affected by the changes:

finfo_buffer()finfo_close()finfo_file()finfo_open()finfo_set_flags()

imap Resources Are Now IMAPconnection Class Objects

The IMAP connection resources were migrated to lớn the IMAPConnection class. Before this, the imap_open()function returned a resource object of typeimap.

To minimize the migration disruption, all functions that accept resources will work with the new IMAPConnection class.

Similar lớn the FTPConnection class, the IMAPConnection class is also declared final to prevent it from being extended & causing backward-compatibility issues.

PostgreSQL Resources Are Now PgSqlConnection, PgSqlResult, và PgSqlLob Objects

PostgreSQL is a PHP database extension that uses resource objects for database connections và results. Three resource objects for PostgreSQL were migrated into class objects.

pgsql link resource lớn the PgSqlConnection class.pgsql result resource khổng lồ the PgSqlResult class.pgsql large object resource to lớn the PgSqlLob class.

All PostgreSQL functions that return & accept resource objects now support the new class objects.

LDAP Resources Are Now LDAPConnection, LDAPResult, và LDAPResultEntry Objects

LDAP (Lightweight Directory Access Protocol) is a PHP extension for accessing Directory Servers, a special database that stores information in a tree structure.

This extension used three resource objects before PHP 8.1. These three objects were now migrated khổng lồ new class instances.

ldap liên kết resource to lớn the LDAPConnection class.ldap result resource lớn the LDAPResult class.ldap result entry resource to the LDAPResultEntry class.

All LDAP functions that return or accept resource objects now accept the new class objects.

Pspell Config Resources Are Now PSpellDictionary, PSpellConfig Class Objects

The Pspell PHP extension lets you kiểm tra spellings & provides word suggestions. It uses pspell and pspell config resource object types with an integer identifier.

These resource objects were replaced by PSpellDictionary and PSpellConfig class objects in PHP 8.1. Here are the functions for creating PSpellDictionary & PSpellConfig objects:

pspell_new()pspell_new_config()pspell_new_personal()pspell_config_create()

Like other migrated resources, all functions that accept pspell and pspell config resources will also accept PSpellDictionary and PSpellConfig.

Deprecations in PHP 8.1

PHP 8.1 deprecated several features, including more than ten already existing PHP functions. Here’s the rundown of what features and functions were deprecated in PHP 8.1.

$Globals Usage Restrictions

The $GLOBALS variable gives references to all global scope variables. It also contains “superglobal” values, including $_GET, $_SERVER, and $_ENV.

PHP 8.1 doesn’t fully deprecate the feature but restricts any modification khổng lồ the $GLOBALS array, such as destroying the array, overwriting its value, và referencing the $GLOBALS variable.

This is because supporting some $GLOBALS variable behaviors requires complex technical work.

If you try to bởi vì these modifications on PHP 8.1, you’ll get a fatal error. Here’s an example of creating a mass change to the $GLOBALS variable:

$GLOBALS = <>;$GLOBALS = <"foo" => 1, "bar" => 2>;$GLOBALS = get_new_vars();$GLOBALS =& $new_vars;list($GLOBALS) = <1>;foreach ($new_var_c as $GLOBALS) unset($GLOBALS);It will result in the following error:

Fatal error: $GLOBALS can only be modified using the $GLOBALS<$name> = $value syntax in ... On line …Note that read-only usage of the $GLOBALS variable and modification to individual array elements are still allowed.

Passing null to non-nullable Internal Function Not Allowed

In PHP, you shouldn’t be able khổng lồ pass null values to lớn non-nullable functions. The restriction worked for user-defined functions in previous PHP versions. Internal functions silently accepted null values.

To maintain consistency, the internal functions don’t accept null values anymore. Passing null khổng lồ non-nullable arguments will return a deprecation notice.

var_dump(str_contains("foobar", null));Deprecated: Passing null to argument of type string is deprecatedThis deprecation is part of a plan lớn remove the functionality. According khổng lồ its RFC, passing null values to non-nullable internal functions will result in TypeError. That will make the internal functions consistent with the current behavior of user-defined functions.

Deprecated Functions

PHP 8.1 also deprecates 11 PHP functions that may affect existing applications.

Deprecated Serializable Interface

PHP classes have their quality serialization logic. For instance, they can prevent specified sensitive data from serialization or reestablish remote vps connections using serialized text. They use three different methods:

__sleep và __wakeup magic methods.Serializable interface, Serializable::serialize, & Serializable:unserialize methods.__serialize and __unserialize magic methods.

__serialize & __unserialize were introduced in PHP 7.4 lớn resolve the issue with the Serializable interface. However, the final plan is khổng lồ remove the Serializable interface in the future PHP versions.

As for PHP 8.1, implementing Serialize interface on a class without the __serialize & __unserialize methods will result in deprecation error.

class chạy thử implements Serializable public function serialize() public function unserialize($data) It will result in the following error message:

Deprecated: kiểm tra implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if tư vấn for old PHP versions is necessary) in ... On line …However, it’s still possible to lớn implement a Serializable interface as long as the class also implements the __serialize và __unserialize magic methods.

class demo implements Serializable{ public function __serialize(): array public function __unserialize(array $data): void public function serialize(): array public function unserialize(string $data): void The Serializable interface decodes the existing serialization payload, & the deprecation notice won’t appear. If there’s no need for PHP 7.3 support in the application, you can replace the Serializable interface with the __serialize và __unserialize magic methods.

Deprecated Autovivification on False

Autovivification is the auto-creation of arrays from false và null values. While this feature is widely used in PHP applications, creating an array automatically from false values is not ideal as it may be a sign of a buggy code.

Therefore, PHP 8.1 deprecates autovivification only for false values. Implementing autovivification on a false variable on PHP 8.1 will result in a deprecation notice.

$arr = false;$arr<> = 2;Deprecated: Automatic conversion of false to array is deprecated inIn future releases, the plan is khổng lồ emit a fatal error on such implementation. As for undefined and null values, autovivification is still allowed.

Deprecated mysqli::get_client_info and mysqli_get_client_info ($param)

Passing parameters lớn the mysqli_get_client_info() function & the mysqli::get_client_info method are used khổng lồ return the MySQLi client version through the client_info string constant.

Prior khổng lồ PHP 8.1, there are four possible options to lớn expose the client_info constant:

mysqli_driver propertiesmysqli propertiesmysqli_get_client_info() functionmysqli::get_client_info method

On the other hand, MySQL Native Driver (MySQLnd) exposes the constant with only two options – a constant and a function call.

Passing parameters khổng lồ the mysqli_get_client_info() function & mysqli::get_client_info is now deprecated in an effort lớn unify the access method.

Using the get_client_info method in the mysqli class now emits a deprecation notice.

$info = $connection->get_client_info();Deprecated: Method mysqli::get_client_info() is deprecated in …Passing parameters to lớn the mysqli_get_client_info() function will also result in a deprecation notice:

$info = mysqli_get_client_info($connection);Deprecated: mysqli_get_client_info(): Passing connection object as an argument is deprecated in …Instead of using the deprecated options, use the mysqli_get_client_info() function without parameters.

$info = mysqli_get_client_info();Deprecated Implicit Incompatible Float to lớn Int Conversion

Type coercion is common in PHP, & in most cases, they are helpful. However, the problem arises when a float number is converted to lớn an integer as it may cause data loss. For example, 1.618 will be converted lớn 1, losing its fractional value.

PHP 8.1 addresses this issue by issuing a deprecation notice if a non-compatible float is implicitly coerced khổng lồ an integer. The notice should prevent unintended fractional value loss.

Below is an example of declaring a function with an int return type. When the function is called, PHP will convert the value of 1.618 lớn an integer, generating a deprecation notice:

function get_integer(): int Return ‘1.618’:get_integer();PHP Deprecated: Implicit conversion from float-string "6.8" lớn int loses precision in ... On line ...The deprecation notice won’t be emitted if the value is an integer-compatible float. According to the RFC, here are the characteristics of an integer-compatible float:

It is a number.It is in the range of a PHP integer.It doesn’t have a fractional part.

Deprecated filter.default và filter.default_options INI Settings

The filter.default INI setting applies a filter to all PHP super globals according to its value. The default value for this setting is filter.default=unsafe_raw – it avoids any filter application on super globals.

The issue with this setting is that it can bring back the magic quotes functionality with filter.default=magic_quotes value. This feature was removed in PHP 5.4 due khổng lồ an unreliable approach for user input sanitation.

To overcome this, assigning any value other than unsafe_raw lớn the filter.default INI setting will emit a PHP deprecation notice at start-up time. There’s no separate warning for filter.default_options, but these two INI settings will be removed in the next major PHP version.

Deprecated date_sunrise and date_sunset

The date_sunrise() and date_sunset() functions return the sunrise & sunset times by taking the default date.default_latitude and date.default_longitude values from the PHP INI settings. Unfortunately, no other functions use these INI settings, & the concept itself makes little sense.

As a result, date_sunrise() & date_sunset()functions are deprecated in PHP 8.1 & replaced by the date_sun_info() function. In accordance with the deprecation, the following INI settings are also deprecated:

date.default_latitudedate.default_longitudedate.sunrise_zenithdate.sunset_senith

Using date_sunrise và date_sunset functions in PHP 8.1 will emit a deprecation warning. However, setting the related INI values or using the ini_set function won’t generate the notice.

Deprecated strptime

The strptime() function parses date or time strings into arrays. Using this function on some Linux distributions like Alpine may result in unexpected behavior as it uses the musl C library. Moreover, this function is locale-based and can be affected if you run other code in a different thread.

The function’s manual recommends the use of date_parse_from_format() as it’s more consistent and doesn’t suffer from the same issues. Other alternatives are also available, like the more object-oriented DateTime::createFromFormat() and IntlDateFormatter::parse() for locale-dependent parsing.

As a result, the strptime() function is deprecated in favor of the more consistent options.

Deprecated strftime & gmstrftime

The strftime() & gmstrftime() functions are similar to strptime(). They format a time & date timestamp according khổng lồ locale settings, except that gmstrftime returns the time in GMT.

Unfortunately, these functions also suffer similar issues as strptime() in terms of the formats supported and behaviors.

These functions are deprecated and replaced by the date()function or DateTime::format(). For locale-dependent use, IntlDateFormatter::format is the ideal replacement.

Deprecated mhash*() or hash extensions

mhash*() functions were brought to PHP by the mhash extension. However, this extension was replaced with hash extension in PHP 5.3. It’s only possible khổng lồ use the functions with a hash extension & the PHP configuration option --enable-mhash.

Since PHP 7.4, the hash extension became a bundle with PHP. The mhash*()functions are still usable because the --enable-mhash option is still supported for compatibility purposes.

However, the PHP team deprecated mhash*() functions in PHP 8.1 và will remove them in PHP 9.0. The use of the following mhash*() functions will return a deprecation warning:

mhashmhash_countmhash_get_block_sizemhash_get_hash_namemhash_keygen_s2k

Since hash is now the default extension, we recommend using hash*() functions instead.

Deprecated PDO::FETCH_SERIALIZE

PDO::FETCH_SERIALIZE is used as a flag for PDO statement fetch methods. Then, PDO is supposed to call unserialize on the data fetched from the database.

However, this functionality is unusable. Plus, with the deprecation of the Serializable interface, the PHP team also decided to deprecate PDO::FETCH_SERIALIZE.

Using PDO::FETCH_SERIALIZE in PHP 8.1 will return a deprecation notice.

Deprecated MySQLi: mysqli_driver->driver_version property

The mysqli_driver->driver_version property in MySQLi extension is supposed to return the driver version value. However, this property hasn’t been updated for 13 years despite many changes lớn the driver.

hotlive |

https://hi88n.com/