Hướng dẫn php 8.2 rfc
Kết luận: Trên đây là các tính năng mới nhất của PHP 8, phiên bản mới phát hàng vào tháng 11.2020. Hy vọng các bạn có thể áp dụng các tính năng mới này trong quá trình làm việc với PHP 8. Tìm hiểu thêm về PHP và các ngôn ngữ lập trình khác qua khóa học lập trình tại T3H.
Show
You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive. PHP 8.2 is released on December 8, 2022. In this post, we'll go through all features, performance improvements, changes and deprecations one by one. Readonly classes RFCReadonly properties were introduced in PHP 8.1. This RFC builds on top of them, and adds syntactic sugar to make all class properties readonly at once. Instead of writing this:
You can now write this:
Functionally, making a class readonly is entirely the same as making every property readonly; but it will also prevent dynamic properties being added on a class:
Note that you can only extend from readonly classes if the child class is readonly as well. PHP has changed quite a lot, and readonly classes are a welcome addition. You can take a look at my video about PHP's evolution if you want to as well: Deprecate dynamic properties RFCI'd say this is a change for the better, but it will hurt a little bit. Dynamic properties are deprecated in PHP 8.2, and will throw an 1 in PHP 9.0:
Keep in mind that classes implementing 2 and 3 will still work as intended:
If you want to learn more about why deprecations are useful and how to deal with them, you can read this followup post on how to deal with deprecations, or you can check out my vlog: New random extension RFCPHP 8.2 adds a new random number generator that fixes a lot of problems with the previous one: it’s more performant, more secure, it’s easier to maintain, and doesn’t rely on global state; eliminating a range of difficult to detect bugs when using PHP’s random functions. There’s a new class called 4, which accepts a randomizer engine. Now you can change that engine, depending on your needs. For example, to differentiate between a production and testing environment.
$post = new Post(/* … */); $post->unknown = 'wrong'; Uncaught Error: Cannot create dynamic property Post::$unknown5, $post = new Post(/* … */); $post->unknown = 'wrong'; Uncaught Error: Cannot create dynamic property Post::$unknown6, and $post = new Post(/* … */); $post->unknown = 'wrong'; Uncaught Error: Cannot create dynamic property Post::$unknown7 as standalone types RFCPHP 8.2 adds three new types — or something that looks like it. We'll avoid going down the rabbit hole of type safety in this post, but technically 5, 6, and 7 could be considered valid types on their own. Common examples are PHP's built-in functions, where 7 is used as the return type for when an error occurs. For example in 2:
Before PHP 8.2, you could already use 7 together with other types as a union; but now it can be used as a standalone type as well:
The same now also goes for 6 and 5.Disjunctive Normal Form Types RFCDNF types allow us to combine and types, following a strict rule: when combining union and intersection types, intersection types must be grouped with brackets. In practice, that looks like this:
In this case, 6 is the DNF type.It's a nice addition, especially since it means that we can now have nullable intersection types, which is probably the most important use case for this feature. Constants in traits RFCYou can now use constants in traits:
You won't be able to access the constant via the trait's name, either from outside the trait, or from inside it. 0You can however access the constant via the class that uses the trait, given that it's public: 1Redact parameters in back traces RFCA common practice in any codebase is to send production errors to a service that keeps track of them, and will notify developers when something goes wrong. This practice often involves sending stack traces over the wire to a third party service. There are case however where those stack traces can include sensitive information such as environment variables, passwords or usernames. PHP 8.2 allows you to mark such "sensitive parameters" with an attribute, so that you don't need to worry about them being listed in your stack traces when something goes wrong. Here's an example from the RFC: 2Fetch properties of enums in const expressions RFCFrom the RFC:
That means that the following code is now valid: 3Return type changes for class Post { public string $title; } // … $post->name = 'Name';9 and class Post { private array $properties = []; public function __set(string $name, mixed $value): void { $this->properties[$name] = $value; } } // … $post->name = 'Name';0Previously, these methods looked like this: 4In PHP 8.2 those method signatures are changed like so: 5This change makes a lot more sense, as it improves static insight possibilities for classes extending from 1 and 2. However, technically, this is a breaking change that might affect custom implementations that extend from either of those two classes.class Post { private array $properties = []; public function __set(string $name, mixed $value): void { $this->properties[$name] = $value; } } // … $post->name = 'Name';3 and class Post { private array $properties = []; public function __set(string $name, mixed $value): void { $this->properties[$name] = $value; } } // … $post->name = 'Name';4 deprecations RFCIn PHP 8.2, using either 3 or 4 will trigger these deprecation notices: 6The RFC argues that these functions have a inaccurate name that often causes confusion: these functions only convert between 7 and 8, while the function name suggest a more broader use. There's a more detailed explanation about the reasoning in the RFC.The alternative? The RFC suggests using 9 instead.Locale-insensitive $rng = $is_production ? new Random\Engine\Secure() : new Random\Engine\Mt19937(1234); $randomizer = new Random\Randomizer($rng); $randomizer->shuffleString('foobar');0 and $rng = $is_production ? new Random\Engine\Secure() : new Random\Engine\Mt19937(1234); $randomizer = new Random\Randomizer($rng); $randomizer->shuffleString('foobar');1 RFCBoth 0 and 1 are no longer locale-sensitive. You can use 4 if you want localized case conversion.Signature changes to several SPL methodsSeveral methods of SPL classes have been changed to properly enforce their correct type signature: 7New $rng = $is_production ? new Random\Engine\Secure() : new Random\Engine\Mt19937(1234); $randomizer = new Random\Randomizer($rng); $randomizer->shuffleString('foobar');5 modifier in PCREYou can now use the 5 modifier ( 7) in 8 functions.ODBC username and password escapingFrom the guide:
The same applies to 0.Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter:Email Subscribe |