How to implement strict type checking of function parameters in PHP?

WBOY
Release: 2024-04-10 15:54:02
Original
1146 people have browsed it

PHP function parameter type strict checking can ensure that the passed parameters are consistent with the declared type. When enabled via declare(strict_types=1), function parameters are required to match the specified type, otherwise a TypeError exception is thrown. Strict checking supports basic types (int, float), composite types (objects, arrays), union types (int|string) and optional types (?int) to improve code robustness and prevent wrong type parameters from being passed.

PHP 函数参数类型严格检查如何实现?

Understand the strict checking of PHP function parameter types

PHP function parameter type checking refers to checking whether the actually passed parameter type is consistent with the function when the function is called. A procedure whose declared parameter types are consistent. The robustness of your code can be improved by strict checking to ensure that the parameters passed to the function are as expected.

Syntax

In PHP 7.0 and above, you can use the Declare statement to strictly check the function parameter type:

declare(strict_types=1);

function add(int $a, int $b) {...}
Copy after login

Pass strict_types=1 After declaring that strict mode is enabled, function add requires two integer type parameters to be passed in, otherwise a TypeError exception will be thrown.

Parameter type

PHP supports strict checking of the following basic types and composite types:

  • Basic types: int, float , bool, string, null
  • ##Composite type:

      Object type :
    • object or Class
    • Array type:
    • array
    • Union type:
    • int|string
    • Optional type:
    • ?int
Actual case

Scenario:

Define a function

calculateArea to calculate the area of ​​geometric figures. Different parameters need to be passed according to different graphics types:

function calculateArea($shape, $params) {...}
Copy after login

Use type checking to prevent parameter errors:

declare(strict_types=1);

function calculateArea(string $shape, array $params): float {...}
Copy after login

Declare and clarify through

strict_types Parameter types prevent passing parameters of the wrong type. For example:

calculateArea(123, []); // 抛出 TypeError 异常
Copy after login

Use union types to improve flexibility:

function calculateArea(string $shape, int|float $radius): float {...}
Copy after login

Union types allow functions to receive different types of parameters according to different situations. For example, to calculate the area of ​​a circle or square:

calculateArea('circle', 5);
calculateArea('square', 10);
Copy after login
Points to note

    Strict mode does not affect existing code, only newly defined functions.
  • An exception is thrown only when the actual parameter type does not match the declared type.
  • If you want to allow a function to return a
  • null value, you can use the optional type ?.
  • When strict mode is enabled, the types must be explicitly specified for all parameters of the function, including optional parameters.

The above is the detailed content of How to implement strict type checking of function parameters in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template