![]() ![]() ![]() Null-safe operator is a new syntax, and cannot be back-ported to older PHP versions. $country = &$customer->getAddress()?->getCountry() Fatal error: Cannot take reference of a nullsafe chain in. The null-safe operator cannot be used with references. If Customer::getAddress() returns a value other than null, the (GeoIP::getAddress()?->getCountry()) chain will be executed, and the return value will be passed to the Address::setCountry() call. $customer->getAddress()?->setCountry((GeoIP::getAddress()?->getCountry())) Note that using braces will make the expression inside the braces execute altogether, as opposed to the left-to-write pattern. Because the null-safe operators are evaluated from left to right, the GetIP::getCountry() method will never be called if the Customer::getAddress() method returns null. If the Customer::getAddress method returns a valid Address object, the GeoIP::getCountry() will be executed, and passed to the Address::setCountry() method call. getAddress()?->setCountry(GeoIP::getCountry()) ![]() Most importantly, it does not prioritize other function calls or other access patterns such as array-access. The null-safe operator is evaluated from left to right. Using the ?-> operator will not make the whole chain null-safe. class Customer Null-safe operator must be present at every step the chain can be short-circuited. Null safe operator silently returns null if the expression to the left side evaluates to null. The syntax is similar to the property/method access operator ( ->), and following the nullable type pattern, the null-safe operator is ?->. The null-safe operator allows reading the value of property and method return value chaining, where the null-safe operator short-circuits the retrieval if the value is null, without causing any errors. Null-safe operator is a new syntax in PHP 8.0, that provides optional chaining feature to PHP. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |