What is new in PHP 8

Blogs ❯❯ PHP

Image could not load

PHP 8 New Features

PHP 8 में नए Updates क्या हैं ?

PHP 8 कई सारे improvement के साथ November 26, 2020 को release की गयी थी। इस Blog में आप में आये new updates like named arguments, union types, attributes, constructor property promotion, match expression, null safe operator, error handling, and consistency के बारे में पढ़ेंगे।

Constructor property promotion

old version में किसी class में non static properties use करने के लिए या तो उन्हें constructor में define करना पड़ता था या बाहर। लेकिन PHP 8 में आप non static properties को directly constructor define करते समय as parameter define करके use में ले सकते हैं। इससे हमें same code बार बार लिखना नहीं पड़ता और coding time भी reduce होता है।

<?php /*PHP 8*/ class MyCalss { public function __construct( public float $x = 0.0, public float $y = 0.0, public float $z = 0.0, ) {} }

<?php /*PHP 7*/ class MyClass { public float $x; public float $y; public float $z; public function __construct( float $x = 0.0, float $y = 0.0, float $z = 0.0 ) { $this->x = $x; $this->y = $y; $this->z = $z; } }

Union Type Declaration

दूसरा सबसे अच्छा update है union type declaration का। PHP 8 से पहले है, class में जो variables define करते थे उनमे किसी भी type की value pass कर सकते थे , जो की अच्छा नहीं था। अब आप variable के type के साथ - साथ एक से ज्यादा type भी define कर सकते है।

<?php /*PHP 8*/ class Number { /* * define variable with two type. * we can pass either int or float type value. */ public function __construct( private int|float $number ) {} } /*It will raise an error*/ // Output : new Number('NaN');

<?php /*PHP 7*/ class Number { /** @var int|float */ private $number; /** * @param float|int $number */ public function __construct($number) { $this->number = $number; } } /*pass value*/ // Output : new Number('NaN'); // Ok

match Expression

यह update PHP 8 में most important और अच्छा update था , because switch break case में अक्सर number string : number और string दोनों से match हो जाती थी , और फिर code भी काफी लिखना पड़ता था। लेकिन में अब आप match expression use करके same operation को आप अच्छी accuracy और better understanding के साथ perform कर सकते हैं।

<?php /*PHP 8*/ echo match (8.0) { '8.0' => "Oh no!", 8.0 => "This is what I expected", }; // This is what I expected

<?php /*PHP 7*/ switch (8.0) { case '8.0': $result = "Oh no!"; break; case 8.0: $result = "This is what I expected"; break; } echo $result; // Oh no!

Nullsafe operator

next , सबसे बड़ा update था , null safe operator . कई बार ऐसी जरूरत आती है जिसमे किसी variable से value निकालने के लिए कई सारी inner conditions की जरूरत पड़ती है।

तो कई सारी conditions की वजाय हम उन conditions को as chain की तरह use कर सकते हैं , किसी भी point पर chain breach होने पर null return होता है।

<?php /*PHP 8*/ $country = $session?->user?->getAddress()?->country; ?>

<?php /*PHP 7*/ $country = null; if ($session !== null) { $user = $session->user; if ($user !== null) { $address = $user->getAddress(); if ($address !== null) { $country = $address->country; } } } ?>

New Classes, Interfaces, and Functions

इसके अलावा कई new Classes, Interfaces, and Functions भी add किये गए हैं -

  • Weak Map : class

  • Stringable : interface

  • str_contains(), str_starts_with(), str_ends_with()

  • fdiv()

  • get_debug_type()

  • get_resource_id()

  • token_get_all() : object implementation

  • New DOM Traversal and Manipulation APIs

तो ये कुछ important & major updates थे , हलांकि सिर्फ यही updates नहीं हैं और भी काफी हैं। बाकी और अधिक जानने के लिए PHP की official website पह https://www.php.net/releases/8.0/en.php पर जाकर PHP 8 के बारे में पढ़ सकते हैं।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook