{"id":148366,"date":"2021-01-06T16:00:20","date_gmt":"2021-01-06T13:00:20","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/8-top-new-features-in-php-8-cloudsavvy-it\/"},"modified":"2021-01-06T16:00:20","modified_gmt":"2021-01-06T13:00:20","slug":"8-top-new-features-in-php-8-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/","title":{"rendered":"#8 Top New Features in PHP 8 \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a3cc51deff3b\" 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-6a3cc51deff3b\" 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\/8-top-new-features-in-php-8-cloudsavvy-it\/#1_Attributes\" >1. Attributes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/#2_Named_Arguments\" >2. Named Arguments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/#4_Union_Types\" >4. Union Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/#5_Match_Expression\" >5. Match Expression<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/#6_The_%E2%80%9CNullsafe%E2%80%9D_Operator\" >6. The \u201cNullsafe\u201d Operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/#7_Stricter_Type_System\" >7. Stricter Type System<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/8-top-new-features-in-php-8-cloudsavvy-it\/#8_JIT\" >8. JIT<\/a><\/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\/8-top-new-features-in-php-8-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#8 Top New Features in PHP 8 \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4047\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b835749967babe94be45e3edd2508d9c\/p\/uploads\/2017\/08\/1ac500da.png\" alt=\"PHP Logo\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>PHP 8 was released at the end of November 2020. It\u2019s a major new version that upgrades the language with additional features and performance improvements.<\/p>\n<p>In this article, we\u2019ll look at eight of the biggest enhancements offered by PHP 8. The full list of additions and changes can be found within the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/releases\/8.0\/en.php\">official documentation<\/a>. The docs also include a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration80.php\">comprehensive migration guide<\/a> for use when updating projects from PHP 7.4.<\/p>\n<h2 id=\"attributes\"><span class=\"ez-toc-section\" id=\"1_Attributes\"><\/span>1. Attributes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Attributes, also known as annotations in other languages, provide a mechanism to add metadata to your codebase. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/language.attributes.php\">Attributes<\/a> can be used with classes, methods, functions and properties. They\u2019re easier to work with than the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/php-annotations.readthedocs.io\/en\/latest\/getting-started.html\">docblock approach<\/a> adopted by many PHP 7 projects.<\/p>\n<p>Attributes are defined as simple classes, themselves annotated with PHP\u2019s built-in <code>Attribute<\/code> attribute:<\/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\">#[Attribute]\n<\/span><span class=\"kw2\">class<\/span> CloudSavvyAttribute <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">protected<\/span> string <span class=\"re0\">$value<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>string <span class=\"re0\">$value<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;;<\/span> value <span class=\"sy0\">=<\/span> <span class=\"re0\">$value<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getValue<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;;<\/span> value<span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>They can then be attached to entities within your codebase:<\/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\">#[CloudSavvyAttribute(\"Example\")]\n<\/span><span class=\"kw2\">class<\/span> CloudSavvyClass <span class=\"br0\">{<\/span>\n    <span class=\"co1\">\/\/ ...<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Attributes are <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/language.attributes.reflection.php\">retrieved via the Reflection API<\/a>. You can inspect and construct attributes defined on a class, method, or other codebase component. Attributes are likely to be most useful within frameworks and libraries, where they\u2019ll help to abstract one-time m<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>ing of app components, such as route definitions and dependency injection wires.<\/p>\n<h2 id=\"named-arguments\"><span class=\"ez-toc-section\" id=\"2_Named_Arguments\"><\/span>2. Named Arguments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/named_params\">Named Arguments<\/a> enable you to pass parameters to methods and functions using an array-like construct. This makes it simpler to skip optional parameters and pass them out-of-order.<\/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=\"kw2\">function<\/span> foo<span class=\"br0\">(<\/span>?string <span class=\"re0\">$a<\/span><span class=\"sy0\">=<\/span><span class=\"kw4\">null<\/span><span class=\"sy0\">,<\/span> ?string <span class=\"re0\">$b<\/span><span class=\"sy0\">=<\/span><span class=\"kw4\">null<\/span><span class=\"sy0\">,<\/span> ?string <span class=\"re0\">$c<\/span><span class=\"sy0\">=<\/span><span class=\"kw4\">null<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>In PHP 7, calling the function above while passing the value <code>\"demo\"<\/code> to <code>$c<\/code> required the following invocation:<\/p>\n<p>In PHP 8, the following syntax can be utilised instead:<\/p>\n<p>This makes function calls with many optional arguments less repetitive and easier to maintain.<\/p>\n<p>Populating a class\u2019s properties with initial values is one of the most frequent roles of a constructor. Code similar to the following is commonplace in PHP 7 codebases:<\/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=\"kw2\">class<\/span> Example <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">protected<\/span> ?string <span class=\"re0\">$Property<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>?string <span class=\"re0\">$Property<\/span><span class=\"sy0\">=<\/span><span class=\"kw4\">null<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;;<\/span> Property <span class=\"sy0\">=<\/span> <span class=\"re0\">$Property<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>PHP 8 adds support for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.decon.php#language.oop5.decon.constructor.promotion\">Constructor Property Promotion<\/a>, a shorthand syntax that lets you combine property definition, typehinting, and population inline in the constructor\u2019s signature. The above example could be rewritten as follows:<\/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=\"kw2\">class<\/span> Example <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">protected<\/span> string <span class=\"re0\">$Property<\/span><span class=\"sy0\">=<\/span><span class=\"kw4\">null<\/span>\n    <span class=\"br0\">)<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This style removes repetition. Its use makes it easier to add additional constructor properties in the future, reducing the number of code sections you\u2019d need to modify.<\/p>\n<h2 id=\"union-types\"><span class=\"ez-toc-section\" id=\"4_Union_Types\"><\/span>4. Union Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>PHP\u2019s type system continues to develop with PHP 8. It\u2019s now possible to hint types as a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/language.types.declarations.php#language.types.declarations.union\">\u201cunion\u201d of two or more<\/a> types, where the type\u2019s value can derive from any of the types in the union.<\/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=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> foo<span class=\"br0\">(<\/span>string<span class=\"sy0\">|<\/span>int <span class=\"re0\">$bar<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> int<span class=\"sy0\">|<\/span>float<span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>In the above contrived example, the <code>foo()<\/code> function accepts both strings and integers. It will return either an integer or a float.<\/p>\n<p>In practice, this abiliy is likely to be most useful when applied to properties and method parameters\u2014it\u2019s good form to accept a variety of input types and normalise to a well-defined single output type.<\/p>\n<h2 id=\"match-expression\"><span class=\"ez-toc-section\" id=\"5_Match_Expression\"><\/span>5. Match Expression<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The new <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/control-structures.match.php\"><code>match<\/code> expression<\/a> is a terser safer alternative to the well-known <code>switch<\/code>. It does not require use of <code>case<\/code> and <code>break<\/code> statements, supports combined conditions, and returns a value instead of entering a new code block. Type coercion is disabled, so <code>1.0<\/code> (float) is not considered equivalent to <code>\"1.0\"<\/code> (string), unlike when using <code>switch<\/code>.<\/p>\n<p>Here\u2019s a typical PHP 7 <code>switch<\/code>:<\/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=\"kw1\">switch<\/span> <span class=\"br0\">(<\/span><span class=\"nu19\">1.0<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">case<\/span> <span class=\"nu19\">1.0<\/span><span class=\"sy0\">:<\/span>\n        <span class=\"re0\">$result<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"Float!\"<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"kw1\">case<\/span> <span class=\"st0\">\"foo\"<\/span><span class=\"sy0\">:<\/span>\n    <span class=\"kw1\">case<\/span> <span class=\"st0\">\"bar\"<\/span><span class=\"sy0\">:<\/span>\n        <span class=\"re0\">$result<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"foobar!\"<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>And, here\u2019s how the same code could look with PHP 8:<\/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=\"re0\">$result<\/span> <span class=\"sy0\">=<\/span> match <span class=\"br0\">(<\/span><span class=\"nu19\">1.0<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"nu19\">1.0<\/span> <span class=\"sy0\">=&gt;;<\/span> <span class=\"st0\">\"Float!\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"foo\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"bar\"<\/span> <span class=\"sy0\">=&gt;;<\/span> <span class=\"st0\">\"foobar!\"<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Once again, the new approach is much more concise. One caveat to point out is that combined conditions syntax values aren\u2019t defined within an array, but rather as a simple comma-delimited set, akin to calling a function.<\/p>\n<h2 id=\"the-nullsafe-operator\"><span class=\"ez-toc-section\" id=\"6_The_%E2%80%9CNullsafe%E2%80%9D_Operator\"><\/span>6. The \u201cNullsafe\u201d Operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another concept borrowed from other languages, PHP 8 now supports <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/nullsafe_operator\">inline null checking<\/a> with automatic short-circuiting. You can build up a chain of checks that will abort, returning <code>null<\/code>, when the evaluation of any element fails.<\/p>\n<p>In PHP 7:<\/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=\"re0\">$photo<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw4\">null<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$user<\/span> <span class=\"sy0\">!==<\/span> <span class=\"kw4\">null<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$user<\/span> <span class=\"sy0\">-&gt;;<\/span> profile <span class=\"sy0\">!==<\/span> <span class=\"kw4\">null<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$user<\/span> <span class=\"sy0\">-&gt;;<\/span> profile <span class=\"sy0\">-&gt;;<\/span> getPhoto<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">!==<\/span> <span class=\"kw4\">null<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n            <span class=\"re0\">$photo<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$user<\/span> <span class=\"sy0\">-&gt;;<\/span> profile <span class=\"sy0\">-&gt;;<\/span> getPhoto<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">-&gt;;<\/span> getPath<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"br0\">}<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>And in PHP 8:<\/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=\"re0\">$photo<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$user<\/span>? <span class=\"sy0\">-&gt;;<\/span> profile? <span class=\"sy0\">-&gt;;<\/span> getPhoto<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>? <span class=\"sy0\">-&gt;;<\/span> getPath<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>PHP 8 again provides a simpler solution, which eliminates nesting. This results in considerably less code to test and maintain.<\/p>\n<h2 id=\"stricter-type-system\"><span class=\"ez-toc-section\" id=\"7_Stricter_Type_System\"><\/span>7. Stricter Type System<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ve already looked at how Union Types add another layer of flexibility to the PHP 8 type system. A number of additional changes in this area enhance the strictness and consistency with which type checking is enforced.<\/p>\n<p>String to number comparisons are <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/string_to_number_comparison\">now saner<\/a>. <code>0<\/code> (integer) is no longer equivalent to <code>\"\"<\/code> (empty string) and comparisons such as <code>123 == \"123abc\"<\/code> now evaluate to <code>false<\/code>, not <code>true<\/code>. These alterations help reduce the chance of comparison errors within your codebase, although some work might be required to ensure legacy projects are compatible.<\/p>\n<p>The type system has also gained an explicit <code>mixed<\/code> type (to indicate any kind of value is compatible), as well as a <code>static<\/code> return type (supporting late static binding). Elsewhere, the signatures of abstract methods in traits are now verified properly.<\/p>\n<p>Altogether, these changes progress PHP\u2019s support for strongly typed programming. Consistent use of type hinting eliminates many bugs that can exist in weakly typed languages, providing developers with greater confidence that the code functions as intended.<\/p>\n<h2 id=\"jit\"><span class=\"ez-toc-section\" id=\"8_JIT\"><\/span>8. JIT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The final entry in our list is a more behind-the-scenes capability, rather than a language feature. PHP 8 adds support for Just-In-Time compilation support, which has the potential to significantly enhance performance.<\/p>\n<p>Long-running scripts hosting repetitive tasks are the most likely to benefit from JIT compilation. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/stitcher.io\/blog\/jit-in-real-life-web-applications\">Benchmarks indicate<\/a> the improvement will be less pronounced in web applications, where PHP is most commonly found. The addition of the JIT may help PHP to extend into other areas of programming though, enhancing the language\u2019s overall appeal and versatility.<\/p>\n<p>The JIT must be enabled using new settings in the <code>php.ini<\/code> configuration file. Detailed guidance on the available values and their effects can be found within the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/jit\">documentation<\/a>; however, the following is advised as a <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> default:<\/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=\"sy0\">&lt;<\/span>code<span class=\"sy0\">&gt;<\/span>opcache<span class=\"sy0\">.<\/span>enable<span class=\"sy0\">=<\/span><span class=\"nu0\">1<\/span>\nopcache<span class=\"sy0\">.<\/span>jit_buffer_size<span class=\"sy0\">=<\/span>100M\nopcache<span class=\"sy0\">.<\/span>jit<span class=\"sy0\">=<\/span><span class=\"nu0\">1255<\/span><span class=\"sy0\">&lt;\/<\/span>code<span class=\"sy0\">&gt;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ve only looked at eight of the most significant additions and alterations in PHP 8. It\u2019s worth looking through the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/releases\/8.0\/en.php\">official release notes<\/a> before starting to use PHP 8 in your projects as there\u2019s many more minor features that we\u2019ve been unable to cover here.<\/p>\n<p>Although most changes are backwards compatible, potential gotchas include the type system enhancements and changes within the standard library. You can find the full <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/migration80.php\">migration guide<\/a> on the PHP documentation site.\n<\/div>\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\/8757\/8-top-new-features-in-php-8\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#8 Top New Features in PHP 8 \u2013 CloudSavvy IT&#8221; PHP 8 was released at the end of November 2020. It\u2019s a major new version that upgrades the language with additional features and performance improvements. In this article, we\u2019ll look at eight of the biggest enhancements offered by PHP 8. The full list of additions&#8230;<\/p>\n","protected":false},"author":1,"featured_media":148367,"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-148366","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\/148366","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=148366"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/148366\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/148367"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=148366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=148366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=148366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}