0 null PHP

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ào

Cú 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[];
4

class 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 ...:...
5

Fatal 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[];
6
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
7

Toá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ức

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
0

Toá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à kiểm tra ba lần

Toá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 null

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6

Toá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[];
7

Chuỗ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ử

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6 sẽ không làm cho toàn bộ chuỗi trở nên vô hiệu

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;
1

Nế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[];
4

Lư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[];
0

Nế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

NULL hay 0 trong PHP?

Để xác định là null hoặc zero bởi. is_int[$var] nếu biến là số hoặc chuỗi số. Để xác định Zero, sử dụng is_numeric[$var] cũng là giải pháp hoặc sử dụng $var === 0. is_null[$var] nếu một biến là NULL

0 có trống trong PHP không?

Những thứ sau đây được coi là rỗng. "" [chuỗi trống] 0 [0 là số nguyên] "0" [0 là chuỗi] . .

0 là đúng hay sai trong PHP?

Khi chuyển đổi thành bool, các giá trị sau được coi là sai. boolean false chính nó. số nguyên 0 [không]

NULL trong PHP là gì?

Null là một kiểu dữ liệu đặc biệt trong PHP chỉ có thể có một giá trị là NULL . Biến có kiểu dữ liệu NULL là biến không có giá trị được gán cho nó.

Chủ Đề