{"id":5479,"date":"2023-09-12T00:36:00","date_gmt":"2023-09-11T19:06:00","guid":{"rendered":"https:\/\/www.wpoven.com\/blog\/?p=5479"},"modified":"2024-01-10T10:30:57","modified_gmt":"2024-01-10T05:00:57","slug":"new-php-8-improvements","status":"publish","type":"post","link":"https:\/\/www.wpoven.com\/blog\/new-php-8-improvements\/","title":{"rendered":"Everything to know about the New PHP 8 update"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\">PHP 8 is scheduled to be launched in December 2020. The new version of PHP comes with many expectations from the user. It is supposed to be built with many varieties of features and improved performance. The changes will make PHP faster and more efficient. Because of the drastic changes in the features, users might need to make some changes in their codes to get PHP 8 functional.&nbsp;<\/span><\/p>\n\n\n<div class=\"wp-block-ub-divider ub-divider-orientation-horizontal\" id=\"ub_divider_13ee018b-8004-4a6d-935b-2ae8e0654de8\"><hr class=\"ub_divider\" ><\/hr><\/div>\n\n\n\n\n<div class=\"wp-block-ub-divider ub-divider-orientation-horizontal\" id=\"ub_divider_13ee018b-8004-4a6d-935b-2ae8e0654de8\"><hr class=\"ub_divider\" ><\/hr><\/div>\n\n\n<p><span style=\"font-weight: 400;\">Its new features include :<\/span><\/p>\n\n\n\n<ul>\n<li><span style=\"font-weight: 400;\">the JIT compiler<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">union types<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">attributes etc.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">There will be many more features that will be added to the list.<\/span><\/p>\n\n\n<div class=\"wp-block-ub-divider ub-divider-orientation-horizontal\" id=\"ub_divider_13ee018b-8004-4a6d-935b-2ae8e0654de8\"><hr class=\"ub_divider\" ><\/hr><\/div>\n\n\n<h2 class=\"wp-block-heading\">Meaning of PHP&nbsp;<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">PHP is a Hypertext Preprocessor that is used for web development.&nbsp; It is very easy to use. And it is still used by highly professional programmers to develop. What makes it the best is that it supports major protocols such as IMAP, POP3, and LDAP. Many previous version of PHP was categorically good for the purpose. PHP 8 comes with many interesting features. Some of them are explained below.<\/span><\/p>\n\n\n<div class=\"wp-block-ub-divider ub-divider-orientation-horizontal\" id=\"ub_divider_13ee018b-8004-4a6d-935b-2ae8e0654de8\"><hr class=\"ub_divider\" ><\/hr><\/div>\n\n\n<div class=\"wp-block-group fre-pros-box is-layout-constrained wp-block-group-is-layout-constrained\"><div class=\"wp-block-group__inner-container\">\n<h2 class=\"wp-block-heading myh2\">New Features in PHP 8<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">The users can expect many new features from the new update. Changes in PHP 8 are made to make it more effective and reliable. The following features are not exhaustive and many more things will be seen when PHP 8 comes out.<\/span><\/p>\n\n\n\n<ul>\n<li><a href=\"#UNION-TYPES-2.0\" data-type=\"internal\" data-id=\"#UNION-TYPES-2.0\">UNION TYPES 2.0<\/a><\/li>\n\n\n\n<li><a href=\"#Just-in-Time-Compiler\" data-type=\"internal\" data-id=\"#Just-in-Time-Compiler\">Just in Time Compiler<\/a><\/li>\n\n\n\n<li><a href=\"#Incompatible-Method-Signatures\" data-type=\"internal\" data-id=\"#Incompatible-Method-Signatures\">Incompatible Method Signatures<\/a><\/li>\n\n\n\n<li><a href=\"#Arrays-Starting-With-a-Negative-Index\" data-type=\"internal\" data-id=\"#Arrays-Starting-With-a-Negative-Index\">Arrays Starting With a Negative Index<\/a><\/li>\n\n\n\n<li><a href=\"#Frequent-Type-Error-for-Internal-Functions\" data-type=\"internal\" data-id=\"#Frequent-Type-Error-for-Internal-Functions\">Frequent Type Error for Internal Functions<\/a><\/li>\n\n\n\n<li><a href=\"#Throw-Expression\" data-type=\"internal\" data-id=\"#Throw-Expression\">Throw Expression<\/a><\/li>\n\n\n\n<li><a href=\"#Weak-Maps\" data-type=\"internal\" data-id=\"#Weak-Maps\">Weak Maps<\/a><\/li>\n\n\n\n<li><a href=\"#Trailing-Commas\" data-type=\"internal\" data-id=\"#Trailing-Commas\">Trailing Commas<\/a><\/li>\n\n\n\n<li><a href=\"#Allowing-::class-on-objects\" data-type=\"internal\" data-id=\"#Allowing-::class-on-objects\">Allowing ::class on objects<\/a><\/li>\n\n\n\n<li><a href=\"#Attributes-v2\" data-type=\"internal\" data-id=\"#Attributes-v2\">Attributes v2<\/a><\/li>\n<\/ul>\n<\/div><\/div>\n\n\n<div class=\"wp-block-ub-divider ub-divider-orientation-horizontal\" id=\"ub_divider_13ee018b-8004-4a6d-935b-2ae8e0654de8\"><hr class=\"ub_divider\" ><\/hr><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"UNION-TYPES-2.0\">1. UNION TYPES 2.0<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Union types can now take values that can be of a different type. These are a combination of two or more types which tells that any one of them can be used. Before coming of PHP 8 the PHP union types could only accept in phpdoc annotations.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">With the new version, you do not have to rely on online documentation. The support will define union types with a T1|T2|.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Union types will be adapting all types except a few (some limitations) :<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The type VOID cannot be supported by Union because it means that the function does not return any value.&nbsp;<\/span><\/p>\n\n\n\n<ul>\n<li><span style=\"font-weight: 400;\">The null type can be backed by the Union type.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">The nullable type unions are also allowed. It can be written using |null, or by using? notation.&nbsp;<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\"><strong>Example:<\/strong><\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>public function foo(Foo|null $foo): void;<br>public function bar(?Bar $bar): void;<\/strong><\/code><\/p>\n\n\n\n<p><a href=\"https:\/\/my.wpoven.com\/r\/892K3T\" target=\"_blank\" rel=\"noopener noreferrer\">WPOven \u2013 The Dedicated WordPress Hosting provides you with \u2013<\/a><\/p>\n\n\n\n<ul>\n<li><strong>FREE SSD Storage<\/strong><\/li>\n\n\n\n<li><strong>FREE SSL CERTIFICATE<\/strong><\/li>\n\n\n\n<li><strong>FREE Business Email<\/strong><\/li>\n\n\n\n<li><strong>FREE Over 100 premium themes and plugins<\/strong><\/li>\n\n\n\n<li><strong>FREE Daily Off-Site Backups<\/strong><\/li>\n\n\n\n<li><strong>FREE Malware Screening &amp; Cleanup&nbsp;<\/strong><\/li>\n\n\n\n<li><strong>FREE WordPress Dev &amp; Server Support<\/strong><\/li>\n\n\n\n<li><strong>Dedicated Server<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Our plans start at $66.63.&nbsp; With dedicated VPS, dedicated resources, server stack, etc<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Just-in-Time-Compiler\">2. Just in Time Compiler<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">There will be landmark changes with JIIT. It translates the intermediate code into machine code. It will be able to bring major changes in data usage and performance. Changes are yet to be disclosed.<\/span><\/p>\n\n\n\n<p>Read our old guide to get a better understanding of<a href=\"https:\/\/www.wpoven.com\/blog\/hhvm-vs-php-7-performance-showdown-wordpress-nginx\/\" target=\"_blank\" rel=\"noopener noreferrer\"> How PHP 7 Vs HHVM compares.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Incompatible-Method-Signatures\">3. Incompatible Method Signatures<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">With PHP 8, the code will prompt with the following message unlike previous versions:&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Fatal error: Declaration of C2::method(int $a) must be compatible with C1::method(array $a) in \/path\/to\/your\/test.php on line 7<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Arrays-Starting-With-a-Negative-Index\">4. Arrays Starting With a Negative Index<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">In PHP&#8217;s previous versions when an array starts with a negative index, many indices would start with 0. But this is not the case with the new version. The array tends to change its behavior<\/span> when starting with a <span style=\"font-weight: 400;\">negative Index.&nbsp;<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Frequent-Type-Error-for-Internal-Functions\">5. Frequent Type Error for Internal Functions<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Internal functions respond in a variety of ways depending on the situation. There has been constant inconsistency in the responses. PHP 8 edits these responses. It will show the message as :<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Fatal error: Uncaught TypeError: strlen(): Argument #1 ($str) must be of type string, object given in \/path\/to\/your\/test.php:4<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Stack trace:<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>0 {main}<br>thrown in \/path\/to\/your\/test.php on line 4<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Throw-Expression\">7. Throw Expression<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">In PHP series throw statement is not allowed to be used where expressions are allowed. PHP8 designs throw statements to be used wherever required. Like in arrow functions, null coalesce, operator, ternary and Elvis operators, etc.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">This can better be explained with the following examples :<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>\/\/ $value is non-nullable.<br>$value = $nullableValue ?? throw new InvalidArgumentException();<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Weak-Maps\">7. Weak Maps<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">&nbsp;It is a pile of data in which keys are weakly referenced. This was made with the objective of removing and destroying the map keys that no longer have to be used. The new<\/span> update will fix areas like memory leaks<span style=\"font-weight: 400;\"> and performance. With PHP 8 the code will show the following results:<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>object(WeakMap)#1 (1) {<br>[0]=&gt;<br>array(2) {<br>[\"key\"]=&gt;<br>object(stdClass)#2 (0) {<br>}<br>[\"value\"]=&gt;<br>int(42)<br>}&lt;<br>}<\/strong><\/code><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">When you want to remove the code, the key will be removed on its own.&nbsp;<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Trailing-Commas\">8. Trailing Commas<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">PHP 8 now gives trailing commas in a parameter list with functions, methods, and closures. As shown below :<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>class Foo {<br>public function __construct(<br>string $x,<br>int $y,<br>float $z, \/\/ trailing comma<br>) {<br>\/\/ do something<br>}<br>}<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Allowing-::class-on-objects\">9. Allowing ::class on objects<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, this feature allows you to use ::class on objects, rather than having to use get_class() on them.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\"><strong>For example:-<\/strong>&nbsp;<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>$foo = new Foo();<br>var_dump($foo::class);<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Attributes-v2\">10. Attributes v2<\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">The annotations are metadata used to define properties of objects, elements, <\/span>files,<span style=\"font-weight: 400;\"> etc. PHP 8 shows it as :<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">&lt;&lt;PhpAttribute&gt;&gt;<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>class ExampleAttribute<br>{<br>public $value;<br>public function __construct($value)<br>{ <br>$this-&gt;value = $value;<br>}<br>}<\/strong><\/code><\/p>\n\n\n\n<p><b>There are several more new functions in the language :<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">It gives a variety of new functions to the language<\/span><\/p>\n\n\n\n<ul>\n<li><span style=\"font-weight: 400;\">str_contains<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">str_starts_with() and str_ends_with()<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">get_debug_type<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>str_contains&nbsp;<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">A previous<\/span> version of PHP was used to provide developers with strstr and strpos.&nbsp; The issue with these two types is that both options are considered very intriguing. It cannot be used easily. PHP 8 gives a <span style=\"font-weight: 400;\">new type for searching inside the strings&nbsp;<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code>str_contains ( string $haystack , string $needle ) : bool<\/code><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The best part is it is very easy to use. You can simply write the following codes :<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>$mystring = 'Managed WordPress Hosting';<br>$findme = 'WordPress';<br>if (str_contains($mystring, $findme)) {<br>echo \"The string has been found\";<br>} else {<br>echo \"String not found\";<br>}<\/strong><\/code><\/p>\n\n\n\n<p><b>str_starts_with() and str_ends_with()<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Two more functions give the opportunity to search inside the string. The options check whether the given string ends or starts with another string.<\/span><\/p>\n\n\n\n<p class=\"round has-background\" style=\"background-color:#f0f0f0\"><code><strong>str_starts_with (string $haystack , string $needle) : bool<br>str_ends_with (string $haystack , string $needle) : bool<\/strong><\/code><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">They can also reply false if the needle is no longer than $haystack<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><b>get_debug_type<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">This function returns the type of variable. It works the same as gettype function. In addition, it also solves class names.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">As the previous versions were not good for type checking. This new improvement is quite beneficial.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The above changes are obvious changes that have been passed. Many more changes and improvements<\/span> are pending on which work is going on. In the coming months, those changes will be clarified. These improvements with PHP8 make<span style=\"font-weight: 400;\"> it very reliable and efficient.&nbsp;<\/span><\/p>\n\n\n<div class=\"wp-block-ub-divider ub-divider-orientation-horizontal\" id=\"ub_divider_13ee018b-8004-4a6d-935b-2ae8e0654de8\"><hr class=\"ub_divider\" ><\/hr><\/div>","protected":false},"excerpt":{"rendered":"<p>PHP 8 is scheduled to be launched in December 2020. The new version of PHP comes with many expectations from the user. It is supposed to be built with many varieties of features and improved performance. The changes will make PHP faster and more efficient. Because of the drastic changes in the features, users might need to make some changes in their codes to get PHP 8 functional.&nbsp;<\/p>\n<p>Its new features include :<\/p>\n<p>the JIT compiler<\/p>\n<p>union types<\/p>\n<p>attributes etc. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.wpoven.com\/blog\/new-php-8-improvements\/\" class=\"more-link\">Read More <i class=\"fa fa-angle-double-right\" aria-hidden=\"true\"><\/i><span class=\"screen-reader-text\"> &#8220;Everything to know about the New PHP 8 update&#8221;<\/span><\/a><\/p>\n","protected":false},"author":25,"featured_media":24748,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[15],"acf":[],"featured_image_src":"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2023\/09\/PHP-8.png","author_info":{"display_name":"snehil","author_link":"https:\/\/www.wpoven.com\/blog\/author\/snehilprakash\/"},"_links":{"self":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts\/5479"}],"collection":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/comments?post=5479"}],"version-history":[{"count":2,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts\/5479\/revisions"}],"predecessor-version":[{"id":24749,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts\/5479\/revisions\/24749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/media\/24748"}],"wp:attachment":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/media?parent=5479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/categories?post=5479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}