{"id":625,"date":"2019-08-21T06:59:14","date_gmt":"2019-08-21T06:59:14","guid":{"rendered":"https:\/\/admin.qltech.com.au\/?post_type=knowledgehub&#038;p=625"},"modified":"2019-11-06T04:42:35","modified_gmt":"2019-11-06T04:42:35","slug":"exciting-features-look-forward-php-7-4","status":"publish","type":"post","link":"https:\/\/steamlinedesign.com\/qltech\/new\/2019\/08\/21\/exciting-features-look-forward-php-7-4\/","title":{"rendered":"Exciting Features to Look Forward in PHP 7.4"},"content":{"rendered":"\n<p>Have you ever wonder? what\u2019s new in PHP. After PHP 7, PHP 7.4 is about to release on November 28th, 2019. No alpha or beta version, PHP 7.4 will be getting released as General Availability in the market.<\/p>\n\n\n\n<p>The most exciting part will be to see the new features in this version that will make&nbsp;<a href=\"https:\/\/www.qltech.com.au\/php-development\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>PHP website development<\/strong><\/a>&nbsp;much reliable and faster.&nbsp;<\/p>\n\n\n\n<p>Though it has already been approved that PHP 8 is the real milestone for PHP performance, still it\u2019s quite far from being released.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/11\/PHP-7.4-1024x576-1024x576.jpg\" alt=\"php 7.4\" class=\"wp-image-2633\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/11\/PHP-7.4-1024x576.jpg 1024w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/11\/PHP-7.4-1024x576-300x169.jpg 300w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/11\/PHP-7.4-1024x576-768x432.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Here we will be focusing on some of the new features that you will be getting with PHP 7.4.<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Constructors &amp; Defaulters<\/strong><\/h3>\n\n\n\n<p>Let\u2019s take a keen look at how typed values are initialised. If the value is scalar types, it is possible to provide the default value:&nbsp;<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo{public int $bar = 4;<br>public ?string $baz = null;<br>public array $list = [1, 2, 3];}<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>If the type is nullable then you can only use \u201cnull\u201d as a default. Well, this is quite obvious to understand but there is some uneven behaviour with the parameter defaults where the following is included:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>function passNull(int $i = null)<br>{&nbsp;<em>\/* \u2026 *\/<\/em>&nbsp;}&nbsp;passNull(null);<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Fortunately, this uneven behaviour with typed properties is not allowed. Also with \u201cobject\u201d or class types, it is quite impossible to get default values. To set their defaults, you will have to use constructors. To initialise the typed values, usage of constructors is obvious:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>private int $a;public function __construct(int $a)<br>{<br>$this-&gt;a = $a;<br>}<br>}<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>However, you will have to make sure that writing uninitialised property outside the constructor is valid. The uninitialised check will not perform until there is something to read within the property.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Uninitialised<\/strong><\/h3>\n\n\n\n<p>Is the following code valid or not?<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>public int $bar;<br>}<br>$foo = new Foo;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>PHP will display an error message when \u201c$bar\u201d is accessed even if its value is not an integer after making an object of \u201cFoo\u201d:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>var_dump($foo-&gt;bar);<br>Fatal error: Uncaught Error: Typed property Foo::$barmust not be accessed before initialization<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>As you can see there is a new type of&nbsp;<strong>\u201cState Variable\u201d<\/strong>&nbsp;called uninitialised.<\/p>\n\n\n\n<p>The value will be \u201cnull\u201d if the \u201c$bar\u201d does not have any value. It is impossible to acknowledge whether the type nullable property was set or is forgotten if the \u201ctypes\u201d are nullable. This is because the new variable \u201cuninitialised\u201d will be introduced in PHP 7.4.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Let\u2019s take a look at some of the major points to remember about uninitialised:<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li>Reading from uninitialised properties will result in displaying a fatal error message.<\/li><li>If the type is non-nullable, you will be able to create an object within the uninitialised property as this state is verified while accessing the property.<\/li><li>Before reading, you can create an uninitialised property.<\/li><li>Unsetting an untyped property will make the value \u201cnull\u201d but if using \u201cunset\u201d on a typed property then the value will be uninitialized.<\/li><\/ul>\n\n\n\n<p>You can keep a note on the below-mentioned code where the non-nullable and uninitialised property is set just after creating the object is valid:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>public int $a;<br>}<br>$foo = new Foo;<br>$foo-&gt;a = 1;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Type validation is done at the time of writing and uninitialised state is only analysed at the time of reading the value of the property. This makes it sure that no invalid type will end up as a property\u2019s value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Inheritance &amp; Type Variance<\/strong><\/h3>\n\n\n\n<p>It does not matter if PHP 7.4 has improved type variance, the typed properties are still the same i.e., invariant. For a better explanation, take a look at the following code:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class A {}<br>class B extends A {}<br>class Foo<br>{<br>public A $prop;<br>}<br>class Bar extends Foo<br>{<br>public B $prop;<br>}<br>Fatal error: Type of Bar::$prop must be A (as in class Foo)<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>If the above-mentioned example does not make any difference then consider taking a look at the following:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>public self $prop;<br>}<br>class Bar extends Foo<br>{<br>public self $prop;<br>}<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>This new\u00a0<strong><a href=\"https:\/\/www.qltech.com.au\/category\/php-development\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">version of PHP<\/a><\/strong>\u00a0will be replacing \u201cself\u201d in the backend with the child class a while before executing the code. The only solution to handle it is mentioned below:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>public Foo $prop;<br>}<br>class Bar extends Foo<br>{<br>public Foo $prop;<br>}<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>If we talk about inheritance, it will be difficult for you to come up with any better use of cases to replace the types of inherited properties.<\/p>\n\n\n\n<p>It is quite strange to see that it is possible to change the type of every individual inherited property. However, it is only possible if the access modifier is changed from \u201cprivate\u201d to \u201cprotected\u201d or \u201cpublic\u201d.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Take a look at the following valid code:<\/strong><\/h5>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>private int $prop;<br>}<br>class Bar extends Foo<br>{<br>public string $prop;<br>}<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>But, changing the type from nullable to non-nullable or vice-versa is not allowed in this version of PHP i.e., 7.4.<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>public int $a;<br>public ?int $b;<br>}<br>class Bar extends Foo<br>{<br>public ?int $a;<br>public int $b;<br>}<br><br>Fatal error: Type of Bar::$a must be int (as in class Foo)<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Types<\/strong><\/h3>\n\n\n\n<p>Let\u2019s see what can be typed and in which form. However, make sure that typed properties work underclasses. For making them work, access modifier or keyword \u201cvar\u201d is required in front of them.<\/p>\n\n\n\n<p>In PHP 7.4, almost every type can be used excluding \u201cvoid\u201d &amp; \u201ccallable\u201d. \u201cvoid\u201d refers to the absence of a value which is quite clear that it cannot be used to type any value. Similarly with \u201ccallable\u201d, it seems nuanced.<\/p>\n\n\n\n<p>The following code shows how a \u201ccallable\u201d looks like in PHP:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>class Foo<br>{<br>public callable $callable;public function __construct(callable $callable)<br>{ \/* \u2026 *\/ }<br>}<br>class Bar<br>{<br>public Foo $foo;<br>public function __construct()<br>{<br>$this-&gt;foo = new Foo([$this, \u2018method\u2019])<br>}<br>private function method()<br>{ \/* \u2026 *\/ }<br>}<br>$bar = new Bar;&nbsp;($bar-&gt;foo-&gt;callable)();<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here, \u201ccallable\u201d is a private \u201cBar: :method\u201d, though it is contexting the \u201cFoo\u201d. Due to this problem, PHP 7.4 do not include \u201ccallable\u201d support.<\/p>\n\n\n\n<p>However, it\u2019s not a big deal as \u201cClosure\u201d is included as a valid type in PHP 7.4 that will call \u201c$this\u201d context when required.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Here\u2019s a list of types that will be available in PHP 7.4:<\/h5>\n\n\n\n<p>\u25cf int<br>\u25cf float<br>\u25cf bool<br>\u25cf aray<br>\u25cf string<br>\u25cf iterable<br>\u25cf object<br>\u25cf self &amp; parent<br>\u25cf ? (nullable)<br>\u25cf Classes &amp; interfaces<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Strict Types &amp; Coercion<\/strong><\/h3>\n\n\n\n<p>PHP is known to be a&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.qltech.com.au\/develop\/web-development\/how-to-empower-web-apps-nowadays-with-laravel-application-development\/\" target=\"_blank\"><strong>dynamic programming language<\/strong>&nbsp;<\/a>to which many developers loves and even hate. If you are passing a string where you expect it an integer, current PHP will approach and will convert it into a string automatically:<\/p>\n\n\n\n<table class=\"wp-block-table is-style-stripes\"><tbody><tr><td>function coerce(int $i)<br>{ \/* \u2026 *\/ }coerce(\u20181\u2019); \/\/ 1<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The same applies on the typed properties. Take a look at the following valid code which is converting \u201c1\u201d to 1.<\/p>\n\n\n\n<table class=\"wp-block-table is-style-regular\"><tbody><tr><td>class Bar<br>{<br>public int $i;<br>}<br>$bar = new Bar;<br>$bar-&gt;i = \u20181\u2019; \/\/ 1<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The best thing is if you do not want this behaviour, you can simply disable it by applying strict types:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>declare(strict_types=1);<br>$bar = new Bar;<br>$bar-&gt;i = \u20181\u2019; \/\/ 1<br><br>Fatal error: Uncaught TypeError:<br>Typed property Bar::$i must be int, string used<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Well, these are some of the important types of properties that you will see in the new update of PHP i.e., PHP 7.4 GA release. Feel free to contact us to know more about&nbsp;<a href=\"https:\/\/www.qltech.com.au\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>PHP web development service<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wonder? what\u2019s new in PHP. After PHP 7, PHP 7.4 is about to release on November 28th, 2019. No alpha or beta version, PHP 7.4 will be getting released as General Availability in the market. The most exciting part will be to see the new features in this version that will make&nbsp;PHP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"lbn_published_stage":false,"lbn_published_production":false,"footnotes":""},"categories":[1932],"tags":[1599,1600,1737],"yst_prominent_words":[276,275,271,270,273,262,268,265,261,260,264,274,281,280,266,267,263,269,272,277],"class_list":["post-625","post","type-post","status-publish","format-standard","hentry","category-php-development","tag-php-7-4","tag-php-7-4-features","tag-php-website-developement"],"yoast":{"focuskw":"php 7.4","title":"Exciting Features to Look Forward in PHP 7.4","metadesc":"PHP web development got new features and improved properties in the latest version. PHP 7.4 can be a game-changing tool for developers in terms of flexibility","linkdex":"70","metakeywords":"","meta-robots-noindex":"","meta-robots-nofollow":"","meta-robots-adv":"","canonical":"","redirect":"","opengraph-title":"","opengraph-description":"","opengraph-image":"","twitter-title":"","twitter-description":"","twitter-image":""},"acf":{"img":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/Exciting-Features-to-Look-forward-in-PHP-7.4.png","long_descrpition":"","written_by":"","feature_image":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/08\/Exciting-Features-to-Look-forward-in-PHP-7.4-1.jpg","posted_by":"QL Tech","is_featured":"0:Non Featured","knowledge_hub_featured_image":false,"short_description":"","icon":false,"service_slider":false},"_links":{"self":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/comments?post=625"}],"version-history":[{"count":18,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"predecessor-version":[{"id":2634,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts\/625\/revisions\/2634"}],"wp:attachment":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/tags?post=625"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/yst_prominent_words?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}