

Type hint in PHP function parameters and return values
source link: https://mlocati.github.io/articles/php-type-hinting.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Here’s a list of all the types you can use in PHP type hinting, with the minimum PHP version since they are available:
array
function foo(array $bar)
PHP 5.1+example
function foo(array $bar = null)
PHP 5.1+example
function foo(): array
PHP 7.0+example
function foo(): ?array
PHP 7.1+example
function foo(bool $bar)
PHP 7.0+example
function foo(bool $bar = null)
PHP 7.0+example
function foo(): bool
PHP 7.0+example
function foo(): ?bool
PHP 7.1+example
callable
function foo(callable $bar)
PHP 5.4+example
function foo(callable $bar = null)
PHP 5.4+example
function foo(): callable
PHP 7.0+example
function foo(): ?callable
PHP 7.1+example
float
function foo(float $bar)
PHP 7.0+example
function foo(float $bar = null)
PHP 7.0+example
function foo(): float
PHP 7.0+example
function foo(): ?float
PHP 7.1+example
function foo(int $bar)
PHP 7.0+example
function foo(int $bar = null)
PHP 7.0+example
function foo(): int
PHP 7.0+example
function foo(): ?int
PHP 7.1+example
iterable
function foo(iterable $bar)
PHP 7.1+example
function foo(iterable $bar = null)
PHP 7.1+example
function foo(): iterable
PHP 7.1+example
function foo(): ?iterable
PHP 7.1+example
object
function foo(object $bar)
PHP 7.2+example
function foo(object $bar = null)
PHP 7.2+example
function foo(): object
PHP 7.2+example
function foo(): ?object
PHP 7.2+example
function foo(self $bar)
PHP 5.0+example
function foo(self $bar = null)
PHP 5.1+example
function foo(): self
PHP 7.0+example
function foo(): ?self
PHP 7.1+example
string
function foo(string $bar)
PHP 7.0+example
function foo(string $bar = null)
PHP 7.0+example
function foo(): string
PHP 7.0+example
function foo(): ?string
PHP 7.1+example
class names
function foo(ClassName $bar)
PHP 5.0+example
function foo(ClassName $bar = null)
PHP 5.1+example
function foo(): ClassName
PHP 7.0+example
function foo(): ?ClassName
PHP 7.1+example
function foo(): void
PHP 7.1+example
boolean, double, integer, resource, static
Never recognized as valid for type hintingexample 1
example 2
example 3
Recommend
-
33
The latest C++ standard is now upon us, so it’s time to have a look at some of its new features. To put one of the new features into context we’re going to have a look at – as the title suggests – multiple function...
-
15
TIL about functools.lru_cache - Automatically caching function return values in Python Oct 27, 2018 This is a short demonstration of how to use the functools.lru_cache
-
11
Gracefully handle mistyping -> as => in function return type #77035
-
4
How to Return Several Values from a Function in C++ Published July 9, 2021 - 0 Comments Functions should take...
-
6
Conversation Copy link ...
-
16
We primarily use function parameters to get outside data inside a function for further processing. Similarly, we return values from a PHP function to get access to processed data outside the function. You can define functions in PHP with or withou...
-
6
In this Python tutorial, we will discuss how to return multiple values from a function in Python. Table Of Contents Let’s Dive into the tutorial.
-
3
Type hint and Decoupling Simple rules to improve type hint in your codebase. While type hinting is important to help improve your codebase usage and testing. For all the fun and joy it brings it...
-
3
How to Return Multiple Values from a Function in JavaScript 1437 views 2 years ago Javascript Return an...
-
7
Contributor Nilstrieb commented
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK