{"id":374919,"date":"2021-12-01T17:01:16","date_gmt":"2021-12-01T14:01:16","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/whats-new-in-php-8-1\/"},"modified":"2021-12-01T17:01:16","modified_gmt":"2021-12-01T14:01:16","slug":"whats-new-in-php-8-1","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/","title":{"rendered":"#What\u2019s New In PHP 8.1?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2555478b5b1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2555478b5b1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#New_Features\" >New Features<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Enums\" >Enums<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Readonly_Properties\" >Readonly Properties<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Intersection_Types\" >Intersection Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Fibers\" >Fibers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#First-Class_Callables\" >First-Class Callables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Using_New_In_Initializers\" >Using New In Initializers<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Final_Class_Constants\" >Final Class Constants<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Others\" >Others<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Backwards_Incompatible_Changes\" >Backwards Incompatible Changes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-new-in-php-8-1\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What\u2019s New In PHP 8.1?&#8221;<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-4047\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"PHP Logo\" width=\"1400\" height=\"600\" \/><\/p>\n<p>PHP 8.1 was released in November 2021 as the latest minor version of the PHP language. It adds several new language features alongside some smaller improvements and performance enhancements. There are a few breaking changes to be aware of but most upgrades from PHP 8.0 should be straightforward.<\/p>\n<h2 id=\"new-features\"><span class=\"ez-toc-section\" id=\"New_Features\"><\/span>New Features<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This year&#8217;s PHP update adds several new features to enhance developer productivity. Here&#8217;s what you&#8217;ll be able to use after you upgrade.<\/p>\n<h3 id=\"enums\"><span class=\"ez-toc-section\" id=\"Enums\"><\/span>Enums<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Enum types are finally part of the PHP language. They let you specify that a value must be one of a set of predefined constants.<\/p>\n<p>Compared with regular constants, enums give you built-in validation when used as method parameters and return values. They may also have backing values and methods that attach extra behavior.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0\n<\/span><span class=\"kw2\">class<\/span> PostStatus <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">const<\/span> Draft <span class=\"sy0\">=<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw2\">const<\/span> Published <span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.0 - Cannot typehint that the value \n<\/span><span class=\"co2\">#           of `$PostStatus` must be defined \n<\/span><span class=\"co2\">#           within `PostStatus`.\n<\/span><span class=\"kw2\">class<\/span> Post <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Headline<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> int <span class=\"re0\">$PostStatus<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1\n<\/span>enum PostStatus <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw1\">case<\/span> Draft <span class=\"sy0\">=<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> Published <span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1 - Now it's impossible to pass an \n<\/span><span class=\"co2\">#           invalid value as the status.\n<\/span><span class=\"kw2\">class<\/span> Post <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Headline<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> PostStatus <span class=\"re0\">$PostStatus<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"readonly-properties\"><span class=\"ez-toc-section\" id=\"Readonly_Properties\"><\/span>Readonly Properties<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The new <code>readonly<\/code> keyword marks a class property as immutable. Readonly properties can only be written to once. Trying to change their value after initialization will throw an error.<\/p>\n<p>This simplifies the creation of simple immutable value objects. Previously you needed to add repetitive methods to a class if you wanted to expose property values without allowing modification. Now you can make the properties <code>public<\/code> without risking unintentional mutations.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0 - This quickly becomes repetitive!\n<\/span><span class=\"kw2\">class<\/span> Post <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">protected<\/span> string <span class=\"re0\">$Headline<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n&nbsp;\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getHeadline<span class=\"br0\">&#040;<\/span><span class=\"br0\">&#041;<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">&#123;<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Headline<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1\n<\/span><span class=\"kw2\">class<\/span> Post <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$Headline<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"intersection-types\"><span class=\"ez-toc-section\" id=\"Intersection_Types\"><\/span>Intersection Types<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>PHP&#8217;s type system now understands intersections. These let you specify that a value must implement multiple interfaces.<\/p>\n<p>This is useful in cases where code needs to call instance methods that are defined by two different interfaces. Previously you needed to create a new interface extending both of the desired ones. This wasn&#8217;t always practical in cases where you couldn&#8217;t modify the target class to implement your new interface.<\/p>\n<p>Intersections are defined by combining two or more types with an ampersand haracter. They add more flexibility to the type system after the introduction of union types in PHP 8.0. One caveat is you can&#8217;t currently use intersection and union types together &#8211; <code>Countable&amp;Stringable|CustomType<\/code> is off limits in this release.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0\n<\/span><span class=\"kw2\">interface<\/span> CountableString <span class=\"kw2\">extends<\/span> Countable<span class=\"sy0\">,<\/span> Stringable <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> countAndReport<span class=\"br0\">&#040;<\/span>CountableString <span class=\"re0\">$value<\/span><span class=\"br0\">&#041;<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw1\">echo<\/span> <span class=\"st0\">&quot;<span class=\"es4\">$value<\/span>: &quot;<\/span> <span class=\"sy0\">.<\/span> <span class=\"kw3\">count<\/span><span class=\"br0\">&#040;<\/span><span class=\"re0\">$value<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1\n<\/span><span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> countAndReport<span class=\"br0\">&#040;<\/span>Countable<span class=\"sy0\">&amp;<\/span>Stringable <span class=\"re0\">$value<\/span><span class=\"br0\">&#041;<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw1\">echo<\/span> <span class=\"st0\">&quot;<span class=\"es4\">$value<\/span>: &quot;<\/span> <span class=\"sy0\">.<\/span> <span class=\"kw3\">count<\/span><span class=\"br0\">&#040;<\/span><span class=\"re0\">$value<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"fibers\"><span class=\"ez-toc-section\" id=\"Fibers\"><\/span>Fibers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Fibers are mechanism to facilitate concurrent execution. They make it easier to implement resumable code blocks that can be suspended from anywhere in the stack.<\/p>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/fibers\">Fibers API<\/a> is relatively low-level. It&#8217;s not expected that end user developers will interact with it on a regular basis. Instead, Fibers will be integrated into libraries that offer async APIs and event loop implementations. They&#8217;re a control flow system that enables simplified higher-level abstractions.<\/p>\n<p>Fibers offer a path to async function calls that look like regular synchronous operations. You can cut out the boilerplate associated with promises and callbacks. Fibers handle suspending and resuming the code at the <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ropriate points, offering one API for blocking and non-blocking implementations of operations.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0 - A promise-based HTTP client\n<\/span><span class=\"re0\">$response<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$httpClient<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">request<\/span><span class=\"br0\">&#040;<\/span><span class=\"st0\">&quot;http:\/\/example.com&quot;<\/span><span class=\"br0\">&#041;<\/span>\n    <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">then<\/span><span class=\"br0\">&#040;<\/span><span class=\"kw2\">function<\/span> <span class=\"br0\">&#040;<\/span>Response <span class=\"re0\">$Response<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$Response<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getBody<\/span><span class=\"br0\">&#040;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw1\">print<\/span> <span class=\"re0\">$response<\/span><span class=\"sy0\">;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1 - An HTTP client using Fibers internally\n<\/span><span class=\"re0\">$response<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$httpClient<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">request<\/span><span class=\"br0\">&#040;<\/span><span class=\"st0\">&quot;https:\/\/example.com&quot;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw1\">print<\/span> <span class=\"re0\">$response<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getBody<\/span><span class=\"br0\">&#040;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"first-class-callables\"><span class=\"ez-toc-section\" id=\"First-Class_Callables\"><\/span>First-Class Callables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration81.new-features.php#migration81.new-features.core.callable-syntax\">Callables are now<\/a> first-class citizens in the PHP language. This means you can directly assign functions to variables, without using the old array syntax or explicit closure creation.<\/p>\n<p>This change will be most useful in functional programming contexts. Passing functions around is cleaner and easier with direct variable assignment.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0\n<\/span><span class=\"re0\">$builtin<\/span> <span class=\"sy0\">=<\/span> Closure<span class=\"sy0\">::<\/span><span class=\"me2\">fromCallable<\/span><span class=\"br0\">&#040;<\/span><span class=\"st0\">&quot;array_filter&quot;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$method<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">&#091;<\/span><span class=\"re0\">$this<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;getData&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"sy0\">;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1\n<\/span><span class=\"re0\">$builtin<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw3\">array_filter<\/span><span class=\"br0\">&#040;<\/span><span class=\"sy0\">...<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$method<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getData<\/span><span class=\"br0\">&#040;<\/span><span class=\"sy0\">...<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>...<\/code> replacing the function&#8217;s parameters is a mandatory part of the syntax. This signals you want to assign the function to the variable, instead of calling the function and assigning its return value.<\/p>\n<h3 id=\"using-new-in-initializers\"><span class=\"ez-toc-section\" id=\"Using_New_In_Initializers\"><\/span>Using New In Initializers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Another <em>new<\/em> feature is the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/new_in_initializers\"><code>new<\/code> keyword in initializers<\/a>. You can now use object instances as default parameter values:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0\n<\/span><span class=\"kw2\">class<\/span> Send<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">News<\/a>letterAction <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">protected<\/span> Mailer <span class=\"re0\">$Mailer<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>?Mailer <span class=\"re0\">$Mailer<\/span><span class=\"sy0\">=<\/span><span class=\"kw4\">null<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mailer<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$Mailer<\/span> ?? <span class=\"kw2\">new<\/span> SmtpMailer<span class=\"br0\">&#040;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1\n<\/span><span class=\"kw2\">class<\/span> SendNewsletterAction <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">protected<\/span> readonly Mailer <span class=\"re0\">$Mailer<\/span><span class=\"sy0\">=<\/span><span class=\"kw2\">new<\/span> SmtpMailer<span class=\"br0\">&#040;<\/span><span class=\"br0\">&#041;<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>new<\/code> keyword will also work with static variables, global constants, and attribute arguments. It gives you more flexibility and convenience when you want an object instance as a default value.<\/p>\n<h2 id=\"final-class-constants\"><span class=\"ez-toc-section\" id=\"Final_Class_Constants\"><\/span>Final Class Constants<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>final<\/code> keyword is now supported for class constants. You can mark individual constants as <code>final<\/code> without sealing the entire class. Previously child classes were free to overwrite the value of constants inherited from parents.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co2\"># PHP 8.0 - Cannot disallow this without \n<\/span><span class=\"co2\">#           making the whole class `final`\n<\/span><span class=\"kw2\">class<\/span> DemoClass <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">const<\/span> DemoConstant <span class=\"sy0\">=<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n<span class=\"kw2\">class<\/span> ChildClass <span class=\"kw2\">extends<\/span> DemoClass <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">const<\/span> DemoConstant <span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co2\"># PHP 8.1 - Just the constant can be `final`.\n<\/span><span class=\"kw2\">class<\/span> DemoClass <span class=\"br0\">&#123;<\/span>\n    final <span class=\"kw2\">public<\/span> <span class=\"kw2\">const<\/span> DemoConstant <span class=\"sy0\">=<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n<span class=\"kw2\">class<\/span> ChildClass <span class=\"kw2\">extends<\/span> DemoClass <span class=\"br0\">&#123;<\/span>\n    <span class=\"co1\">\/\/ Raises a fatal error<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">const<\/span> DemoConstant <span class=\"sy0\">=<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"others\"><span class=\"ez-toc-section\" id=\"Others\"><\/span>Others<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Besides the big ticket items listed above, PHP 8.1 also adds a few convenience features that help to made code more self-documenting. You can write octal numbers with an explicit notation, such as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration81.new-features.php#migration81.new-features.core.octal-literal-prefix\"><code>0o14<\/code><\/a>, and use the new <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/language.types.declarations.php#language.types.declarations.never\"><code>never<\/code> return type<\/a> to denote functions that will throw an exception or exit the current script. This helps inform dead code detection in static analysis software.<\/p>\n<p>There are a few new functions in the standard library. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/function.array-is-list.php\"><code>array_is_list()<\/code> lets you check<\/a> whether an array is a list (<code>[\"foo\", \"bar\"]<\/code>) or associative (<code>[\"foo\" =&gt; \"bar\"]<\/code>), providing a built-in implementation for a function that&#8217;s commonly found in existing userland code. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/function.fsync\"><code>fsync()<\/code><\/a> and <code>fdatasync()<\/code> let you synchronize file pointer changes back to the underlying storage <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration81.new-functions.php\">Sodium support<\/a> for the XChaCha20 and Ristretto255 hashing algorithms has been added too.<\/p>\n<p>Finally, this release brings the prospect of performance improvements. Optimizations including a class inheritance cache could deliver a free performance boost of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/stitcher.io\/blog\/php-81-performance-in-real-life\">up to 8%<\/a>. This would be most noticeable in heavily object-oriented codebases with many parent-child relationships. PHP will now cache these links so it&#8217;s not continually rebuilding them.<\/p>\n<h2 id=\"backwards-incompatible-changes\"><span class=\"ez-toc-section\" id=\"Backwards_Incompatible_Changes\"><\/span>Backwards Incompatible Changes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As a minor version bump, there are a few breaking changes and deprecation to be aware of before you upgrade. Here are the ones most likely to be an issue in your codebase:<\/p>\n<ul>\n<li>Overwriting the <code>$_GLOBALS<\/code> array is no longer allowed. You can still write to individual keys but can&#8217;t reassign <code>$_GLOBALS<\/code> itself.<\/li>\n<li>If you extend a built-in class, your overridden methods must declare a return type that&#8217;s compatible with the original one. A deprecation warning will be emitted if you specify an incompatible type. A <code>#[ReturnTypeWillChange]<\/code> attribute is currently supported as a way to avoid this for libraries that need to support multiple PHP versions.<\/li>\n<li>Passing <code>null<\/code> to non-nullable parameters of internal functions has been deprecated. This was previously allowed but was inconsistent with typehint enforcement for user-created functions.<\/li>\n<li>Implicit casts from <code>int<\/code> to <code>float<\/code> have been deprecated &#8211; this affects scenarios such as <code>$array[10.1]<\/code>, where the mantissa will be lost as arrays must have integer keys.<\/li>\n<li>As part of PHP&#8217;s ongoing work to remove <code>resource<\/code> types, several function groups now work with new objects instead of resources. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration81.incompatible.php#resource-to-object-migration\">These changes<\/a> apply to the FileInfo, FTP, IMAP, LDAP, PgSQL and PSpell APIs.<\/li>\n<li>Functions that work with HTML entities such as <code>htmlspecialchars()<\/code> and <code>htmlentities()<\/code> now escape apostrophes by default, so <code>'<\/code> will become <code>&#039;<\/code>.<\/li>\n<li>Directly calling static methods on traits is deprecated; you should call the method on a class that uses the trait instead.<\/li>\n<\/ul>\n<p>Other <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration81.incompatible.php\">removals and deprecations<\/a> affect individual APIs including PDO, MySQLi, and Standard. You should review the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration81.php\">entire migration guide<\/a> before you upgrade your project.<\/p>\n<p>Taken together, this cycle&#8217;s changes are fairly innocuous. Codebases that were already conformant with modern PHP best practices shouldn&#8217;t experience too many issues when moving to 8.1. Popular frameworks including Symfony and Laravel already support PHP 8.1 too.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>PHP 8.1 adds many new features that make the development experience easier and more streamlined. Enums have long been a missing piece of the type system while readonly properties and <code>new<\/code> in initializers will make it quicker to write new classes.<\/p>\n<p>Fibers help to make async PHP more approachable while first-class callables facilitate streamlined function references when practicing functional programming techniques. All these changes further mature PHP as a flexible language that offers strong safety guarantees for your code while still being simple to work with.<\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14958\/whats-new-in-php-8-1\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What\u2019s New In PHP 8.1?&#8221; PHP 8.1 was released in November 2021 as the latest minor version of the PHP language. It adds several new language features alongside some smaller improvements and performance enhancements. There are a few breaking changes to be aware of but most upgrades from PHP 8.0 should be straightforward. New Features&#8230;<\/p>\n","protected":false},"author":1,"featured_media":374920,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-374919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/374919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=374919"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/374919\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/374920"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=374919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=374919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=374919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}