10

Type hint in PHP function parameters and return values

 3 years ago
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.
neoserver,ios ssh client

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(): arrayPHP 7.0+example function foo(): ?arrayPHP 7.1+example

function foo(bool $bar)PHP 7.0+example function foo(bool $bar = null)PHP 7.0+example function foo(): boolPHP 7.0+example function foo(): ?boolPHP 7.1+example

callable

function foo(callable $bar)PHP 5.4+example function foo(callable $bar = null)PHP 5.4+example function foo(): callablePHP 7.0+example function foo(): ?callablePHP 7.1+example

float

function foo(float $bar)PHP 7.0+example function foo(float $bar = null)PHP 7.0+example function foo(): floatPHP 7.0+example function foo(): ?floatPHP 7.1+example

function foo(int $bar)PHP 7.0+example function foo(int $bar = null)PHP 7.0+example function foo(): intPHP 7.0+example function foo(): ?intPHP 7.1+example

iterable

function foo(iterable $bar)PHP 7.1+example function foo(iterable $bar = null)PHP 7.1+example function foo(): iterablePHP 7.1+example function foo(): ?iterablePHP 7.1+example

object

function foo(object $bar)PHP 7.2+example function foo(object $bar = null)PHP 7.2+example function foo(): objectPHP 7.2+example function foo(): ?objectPHP 7.2+example

function foo(self $bar)PHP 5.0+example function foo(self $bar = null)PHP 5.1+example function foo(): selfPHP 7.0+example function foo(): ?selfPHP 7.1+example

string

function foo(string $bar)PHP 7.0+example function foo(string $bar = null)PHP 7.0+example function foo(): stringPHP 7.0+example function foo(): ?stringPHP 7.1+example

class names

function foo(ClassName $bar)PHP 5.0+example function foo(ClassName $bar = null)PHP 5.1+example function foo(): ClassNamePHP 7.0+example function foo(): ?ClassNamePHP 7.1+example

function foo(): voidPHP 7.1+example

boolean, double, integer, resource, static

Never recognized as valid for type hintingexample 1
example 2
example 3


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK