Home > Backend Development > PHP Tutorial > The role and use of type hints in PHP

The role and use of type hints in PHP

墨辰丷
Release: 2023-03-31 07:54:02
Original
1556 people have browsed it

This article mainly introduces the role and usage of type hints in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Overview

Starting from PHP5, we can use type hints to specify the parameter types that the function receives when defining the function. If the parameter type is specified when defining a function, then when we call the function, if the type of the actual parameter does not match the specified type, PHP will generate a fatal error (Catchable fatal error).

Class names and arrays

When defining functions, PHP only supports two type declarations: class names and arrays. Class name table name The actual parameter received by this parameter is the object instantiated by the corresponding class, and the array indicates that the actual parameter received is an array type. The following is an example:

The code is as follows:

function demo(array $options){
  var_dump($options);
}
Copy after login


When defining the demo() function, the parameter type received by the function is specified as an array. If when we call a function, the parameter passed in is not an array type, for example, a call like the following:

The code is as follows:

$options='options';
demo($options);
Copy after login

Then the following error will be generated:

Catchable fatal error: Argument 1 passed to demo() must be of the type array, string given,

You can use null as the default parameter

Note

One thing that needs special attention is that PHP only supports two types of type declarations. Any other scalar type declarations are not supported. For example, the following code will generate an error:

The code is as follows:

function demo(string $str){
}
$str="hello";
demo($str)
Copy after login

When we run the above code, string will be treated as a class name, so the following error will be reported:
Catchable fatal error: Argument 1 passed to demo() must be an instance of string , string given,

Summary

Type declaration is also an advancement in object-oriented PHP, especially when catching exceptions of a specified type. It is very useful.
Using type declarations can also increase the readability of the code.
However, since PHP is a weakly typed language, using type declarations is contrary to the original intention of PHP design.
Whether to use type declarations or not is a matter of opinion for everyone. I am a noob :).

The above is the entire content of this article. I hope it will be helpful to everyone's learning.

Related recommendations:

php methods for string operations

php for file operations and strings Encryption method

php method of operating dates and strings

The above is the detailed content of The role and use of type hints 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