Toán tử an toàn null cho phép đọc giá trị của chuỗi giá trị trả về thuộc tính và phương thức, trong đó toán tử an toàn null ngắt mạch truy xuất nếu giá trị là
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4 mà không gây ra bất kỳ lỗi nàoCú pháp tương tự như toán tử truy cập thuộc tính/phương thức [
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
5] và theo mẫu kiểu nullable, toán tử an toàn null là class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
6$foo?->bar?->baz;
Toán tử an toàn null âm thầm trả về
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4 nếu biểu thức ở phía bên trái ước tính là class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
Trong đoạn mã trên, giá trị trả về của phương thức
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
9 là nullable; Chuỗi
Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
2 không phải là "null safe", bởi vì giá trị trả về của Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
3 có thể là class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4 và PHP đưa ra lỗi khi cố gọi phương thức Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
5Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
Để truy cập địa chỉ một cách an toàn, cần phải kiểm tra các giá trị trả về của
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4 trước khi tiếp tục truy cập vào giá trị trả vềclass Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
6class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
7Toán tử an toàn null giải quyết vấn đề này bằng cách rút ngắn quyền truy cập thuộc tính/phương thức và trả về
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4 ngay lập tức nếu phía bên trái của toán tử là class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4, mà không thực hiện phần còn lại của biểu thứcclass Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
0Toán tử an toàn null
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
6 có thể giúp giảm thiểu việc kiểm tra quá mức class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
60 và ternaryToán tử an toàn không có giá trị đã được thêm vào PHP 8. 0 giữa bản phát hành alpha 3 và bản beta đầu tiên, ngay trong khoảng thời gian PHP 8. 0 đạt đến đóng băng tính năng của nó
Toán tử an toàn null là chỉ đọc. Nó không thể ghi/gán giá trị từ nó
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
3Đoạn mã này cố ghi vào thuộc tính
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
61 của đối tượng Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
1. Điều này không được phép với toán tử an toàn nullclass Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
6Toán tử an toàn null có thể được xâu chuỗi và toàn bộ biểu thức sẽ bị rút ngắn từ toán tử an toàn null đầu tiên gặp phải null
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
7Chuỗi cuộc gọi này không an toàn. PHP sẽ dừng và ngay lập tức trả về
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4 nếu bất kỳ toán tử trả về an toàn null nào đánh giá là class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4{code-ok} Người vận hành Null-safe phải có mặt ở mọi bước mà chuỗi có thể bị đoản mạch. Sử dụng toán tử
6 sẽ không làm cho toàn bộ chuỗi trở nên vô hiệuclass Customer { public function getAddress[]: ?Address {} } class Address { public function getCountry[]: string {} } $country = $customer->getAddress[]->getCountry[];
Toán tử an toàn null được đánh giá từ trái sang phải. Quan trọng nhất, nó không ưu tiên các lệnh gọi hàm khác hoặc các mẫu truy cập khác, chẳng hạn như truy cập mảng
$foo?->bar?->baz;
1Nếu phương thức
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
66 trả về một đối tượng Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
1 hợp lệ, thì class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
68 sẽ được thực thi và được chuyển đến lệnh gọi phương thức class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
69. Bởi vì các toán tử an toàn null được đánh giá từ trái sang phải, phương thức class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
70 sẽ không bao giờ được gọi nếu phương thức class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
9 trả về class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4Lưu ý rằng việc sử dụng dấu ngoặc nhọn sẽ làm cho biểu thức bên trong dấu ngoặc nhọn thực thi hoàn toàn, ngược lại với mẫu viết từ trái sang phải
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
0Nếu
class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
9 trả về một giá trị khác với class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
4, chuỗi class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
75 sẽ được thực thi và giá trị trả về sẽ được chuyển đến lệnh gọi class Customer {
public function getAddress[]: ?Address {}
}
class Address {
public function getCountry[]: string {}
}
$country = $customer->getAddress[]->getCountry[];
69