


What are php type constraints? Introduction and usage of php type constraints
Aug 08, 2018 pm 05:14 PMThe so-called type constraint means that when defining a variable, its type must be specified, and the variable can only store data of this type in the future. In this article, I will introduce you to PHP type constraints and usage.
Introduction to php type constraints
PHP is a weakly typed language. Its characteristic is that there is no need to specify a type for a variable, and any type can be stored afterwards. , of course, this is also one of the key points for rapid development using PHP. But since PHP5, we can use type constraints in function (method) parameters.
The parameters of the function can be specified in the following range:
1. It must be an object (specify the name of the class in the function prototype);
2. Interface;
3. Array (from PHP 5.1);
4. Callable (from PHP 5.4).
5. If you use NULL as the default value of the parameter, you can still use NULL as the actual parameter when calling the function.
6. If a class or interface specifies type constraints, so do all its subclasses or implementations.
Note: Before PHP7, type constraints could not be used for scalar types such as int or string. Traits are not allowed either.
Usage of php type constraints:
The following is the official example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Parameters of function calls and defined parameter types In case of inconsistency, a catchable fatal error is thrown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Type constraints are not only used in member functions of classes, but can also be used in functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Type constraints allow NULL values:
1 2 3 4 5 6 7 8 9 10 11 |
|
PHP7
Scalar type declaration (PHP 7)
There are two modes for scalar type declaration: mandatory (default) and strict mode.
The following type parameters can now be used (whether in forced mode or strict mode):
1, string (string),
2, integer (int),
3. Floating point number (float),
4. Boolean value (bool).
They extend other types introduced in PHP5: class names, interfaces, arrays and callback types.
1 2 3 4 5 6 7 8 |
|
The above example will output: int(9)
To use strict mode, a declare declaration directive must be placed at the top of the file. This means that scalars are strictly declared configurable on a file basis. This directive not only affects the type declaration of parameters, but also affects the return value declaration of functions.
Recommended related articles:
Introduction to type constraints in PHP, introduction to PHP type constraints
Sharing of code ideas for type constraints in PHPHow to use type constraints to limit php function types
The above is the detailed content of What are php type constraints? Introduction and usage of php type constraints. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
