PHP Nhận loại đối tượng

Hàm

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 đi kèm với PHP 8. 0 là một nỗ lực để cải thiện hàm
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
5 trả về các giá trị không nhất quán và không mở rộng để tiết lộ tên lớp của các đối tượng

Một trong những trường hợp sử dụng của hàm này là sử dụng nó như một cách dễ dàng để giải thích một biến không mong muốn trong nhật ký lỗi hoặc thông báo ngoại lệ vì kiểu trả về của

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
5 thường không đủ dài để có nghĩa

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 so với
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
5

Các phần sau đây sẽ giải thích sự khác biệt giữa hàm

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 mới và giá trị trả về của hàm
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
5 hiện tại

các loại vô hướng

TypeExample valuegettype[]get_debug_type[]String
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
1
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
2_______02Arrays
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
5
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
5Null
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
7
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
8
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
7Integers
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
0
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
1
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
2Float
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
3
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
4
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
5Boolean
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
6
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
7
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
8Boolean_______99
if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}
7_______98

Lưu ý cách hàm

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 mới trả về các kiểu chính xác mà bạn sử dụng khi nhập vô hướng

Đối tượng lớp và chức năng ẩn danh

LoạiGiá trị ví dụgettype[]get_debug_type[]Đối tượng lớp
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
23
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
24
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
25Đối tượng lớp
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
26
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
24
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
28Đối tượng lớp
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
29
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
24
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
41Close
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
42
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
24
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
44Lớp ẩn danh
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
45
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
24
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
47Lớp con ẩn danh
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
48
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
24_______250

Như bạn có thể thấy,

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 đang bắt đầu trở nên hữu ích hơn ở đây, bởi vì nó báo cáo tên của lớp hoặc các chi tiết khác về các lớp ẩn danh một cách hữu ích

Nếu một bao đóng được thông qua [là một đối tượng bên trong], thì

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 sẽ trả về giá trị đó là
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
44

Khi một đối tượng lớp ẩn danh được chuyển, nó trả về

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
47 cho các lớp không có cha/giao diện hoặc tên cha/giao diện, theo sau là
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
55

Đây là lúc hàm

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 trở nên khá hữu ích vì thay vì gọi
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
57 và
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
58,
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 trực tiếp trả về một tên hữu ích cho đối tượng đã cho. Điều này có thể giúp giảm thiểu mã soạn sẵn và mã sưng lên khi các ngoại lệ được đưa ra và các lỗi được ghi lại

Tài nguyên

LoạiGiá trị ví dụgettype[]get_debug_type[]Streams
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
50
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
51
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
52Trình xử lý cuộn tròn
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
53
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
51
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
55Trình xử lý cuộn tròn đã đóng
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
56
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
57
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
57Tài liệu XML
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
59
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
51
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
41.
if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
51____243

Đối với tất cả các loại tài nguyên trong PHP, hàm

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 cũng sẽ trả về loại tài nguyên đó

Trường hợp sử dụng

Khi bạn muốn xuất một thông báo lỗi dài dòng về một loại không mong muốn, thay vì thực hiện tra cứu loại xấu, thường bỏ qua các loại biến khác, giờ đây bạn có thể dễ dàng lấy loại biến đã truyền

trước đây

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
1

Hiện nay

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}

Polyfill

if [!function_exists['get_debug_type']] {
    function get_debug_type[$value]: string {
        switch [true] {
            case null === $value: return 'null';
            case \is_bool[$value]: return 'bool';
            case \is_string[$value]: return 'string';
            case \is_array[$value]: return 'array';
            case \is_int[$value]: return 'int';
            case \is_float[$value]: return 'float';
            case \is_object[$value]: break;
            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
            default:
                if [null === $type = @get_resource_type[$value]] {
                    return 'unknown';
                }

                if ['Unknown' === $type] {
                    $type = 'closed';
                }

                return "resource [$type]";
        }

        $class = \get_class[$value];

        if [false === strpos[$class, '@']] {
            return $class;
        }

        return [get_parent_class[$class] ?: key[class_implements[$class]] ?: 'class'].'@anonymous';
    }
}

Trên đây là một polyfill mà bạn có thể sử dụng trong mã của mình nếu bạn muốn đưa hàm

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 vào mã của riêng mình mà không thể yêu cầu PHP 8. 0 để chạy. Đoạn mã này là từ Symfony's PHP 8. 0 polyfill

Tác động tương thích ngược

if [![$foo instanceof Foo]] { 
    throw new TypeError[
        sprintf[
            'Parameter 1 is expected to be of type "%s", got "%s" instead.',
            Foo::class,
            get_debug_type[$foo]
        ]
    ];
}
4 là một chức năng mới. Trừ khi bạn đã khai báo một hàm có cùng tên trong không gian tên chung, điều này sẽ không gây ra bất kỳ sự cố BC nào

Nhận loại trong PHP là gì?

gettype[giá trị $ hỗn hợp]. chuỗi . Trả về loại giá trị của biến PHP .

Đối tượng kiểu dữ liệu PHP là gì?

Trong PHP, Đối tượng là kiểu dữ liệu phức hợp [cùng với mảng] . Giá trị của nhiều loại có thể được lưu trữ cùng nhau trong một biến duy nhất. Đối tượng là một thể hiện của lớp dựng sẵn hoặc do người dùng định nghĩa. Ngoài các thuộc tính, lớp xác định chức năng liên quan đến dữ liệu.

Làm cách nào để kiểm tra loại dữ liệu cụ thể trong PHP?

Sử dụng hàm "gettype[]" , bạn có thể xác định loại biến. Trong gettype[], bạn chỉ cần truyền biến mà bạn muốn kiểm tra và hàm này trả về kiểu biến là một chuỗi.

Làm cách nào để lấy giá trị của một đối tượng trong PHP?

Hàm get_object_vars[] là một hàm có sẵn trong PHP được sử dụng để lấy các thuộc tính của đối tượng đã cho.

Chủ Đề