Syntax Comparison
The language is defined by a syntax that is based on the grammar of popular static code analysis tools: PHPStan and Psalm.
PHPStan: https://phpstan.org
Psalm: https://psalm.dev
Phan: https://github.com/phan
phpDocumentor: https://docs.phpdoc.org
General table across all type parsing capabilities
Methodology
Each verdict below is the answer of the tool's own parser, asked directly, so the table says what the tools do rather than what they are said to do.
Tool | Version | A type is supported when |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Basic Types
Below is a list of simple, logical and other common types.
Class or type name (including FQN)
Fully\Qualified\Name
| |||||
|---|---|---|---|---|---|
T | U | V
| |||||
T & U & V
| |||||
?T
| |||||
(T | U)[]
| |||||
User[]
| |||||
ExampleShape['key']
ClassName::CONSTANT[0]
| |||||
ExampleCollection<array-key, User>
| |||||
| |||||
ExampleCollection<in array-key, out User>
| |||||
| |||||
ExampleCollection<*>
| |||||
| |||||
$this
| |||||
ClassName::CONSTANT_NAME
| |||||
Prefixed class constant mask types
ClassName::CONSTANT_*
| |||||
Non-prefixed class constant mask types
ClassName::*
| |||||
Suffixed class constant mask types
ClassName::*_SUFFIX
| |||||
Class constant mask types carrying both ends
ClassName::PREFIX_*_SUFFIX
| |||||
JSON_*
| |||||
Suffixed global constant mask types
*_SUFFIX
| |||||
Namespaced constant mask types
Path\To\JSON_*
Path\To\*
| |||||
int /* comment */ | string
| |||||
Conditional Types
Below is a list of conditional types.
Conditional positive equality types
T is A ? B : C
| |||||
|---|---|---|---|---|---|
| |||||
Conditional negative equality types
T is not A ? B : C
| |||||
| |||||
$var is A ? B : C
$var is not A ? B : C
| |||||
| |||||
Conditional referenced types in Yoda-style
A is $var ? B : C
A is not $var ? B : C
| |||||
Functions in conditional types
foo() is A ? B : C
foo() is not A ? B : C
| |||||
| |||||
Functions in conditional types in Yoda-style
A is foo() ? B : C
A is not foo() ? B : C
| |||||
Literal Types
Below is a list of literal types/lexemes.
Boolean
true
false
| |||||
|---|---|---|---|---|---|
null
| |||||
'single-quoted string'
| |||||
"double-quoted string"
| |||||
Escape sequences in a double-quoted string literals
"string with \n new line \n delimiters"
| |||||
Hexadecimal sequences in a double-quoted string literals
"\xDE\xAD\xBE\xEF"
| |||||
Unicode sequences in a double-quoted string literals
"This is smile \u{1F60A}"
| |||||
42
| |||||
BigInteger (
9999999999999999999
| |||||
| |||||
Integer literals in binary format
0b10101010
| |||||
| |||||
Integer literals in octal format
0o42
| |||||
| |||||
Integer literals in legacy octal format
042
| |||||
| |||||
Integer literals in hexadecimal format
0xDEAD_BEEF
| |||||
| |||||
Underscore (
42_04
| |||||
Explicitly signed number literals
+42
| |||||
0.42
| |||||
Float literals without leading zero
.42
| |||||
Float literals without trailing zero
42.
| |||||
Float literals in scientific notation
2e2
| |||||
Float literals in scientific notation carrying a signed exponent
-1.5e+3
| |||||
Hexadecimal integer literals carrying an
0x42e2
| |||||
| |||||
Shape Types
Below is a list of grammar of shaped types.
array {
key: ValueType
}
| |||||
|---|---|---|---|---|---|
Trailing comma in explicit shape types
array {
key: ValueType,
}
| |||||
array {
ValueType
}
| |||||
| |||||
Trailing comma in implicit shape types
array {
ValueType,
}
| |||||
Optional keys in explicit shape types
array {
key?: ValueType
}
| |||||
array {}
| |||||
array { ... }
| |||||
| |||||
array {
key: ValueType,
...
}
| |||||
array {
ValueType,
...
}
| |||||
array {
...<array-key, ValueType>
}
| |||||
array {
Path\To\ClassName::CONSTANT_NAME: string,
}
| |||||
array {
Path\To\ClassName::PREFIX_*: string,
JSON_*: string,
}
| |||||
array {
'some key': ValueType,
}
| |||||
array {
0: ValueType,
}
| |||||
Shapes of an arbitrary type name
Custom\ObjectType {
key: ValueType
}
| |||||
| |||||
Callable Types
Below is a list of grammar of callable (function) types.
callable()
| |||||
|---|---|---|---|---|---|
callable(): Type
| |||||
Callable with typed parameters
callable(Type): T
| |||||
Trailing comma in the parameter list
callable(Type,): T
| |||||
Callable with optional parameters
callable(Type=): T
| |||||
Callable with named parameters
callable(Type $name): T
| |||||
Callable with optional named parameters
callable(Type $name=): T
| |||||
Callable with output parameters
callable(T&): T
| |||||
| |||||
Callable with output optional parameters
callable(T&=): T
| |||||
| |||||
Callable with output optional named parameters
callable(T &$name=): T
| |||||
Callable with suffixed variadic parameters
callable(Type...): T
| |||||
Callable with named variadic parameters
callable(Type ...$name): T
| |||||
Callable with output named variadic parameters
callable(Type &...$name): T
| |||||
Callable with prefixed variadic parameters
callable(...Type): T
| |||||
| |||||
Callable with optional variadic parameters
callable(Type...=): T
callable(Type ...$name=): T
| |||||
|
| ||||
| |||||
Callable carrying the variadic marker twice
callable(...Type ...$name): T
| |||||
|
| ||||
| |||||
callable(): $this
| |||||
Callable declaring template parameters
callable<T>(T): T
| |||||
Upper bound of a template parameter
callable<T of Some>(T): T
callable<T as Some>(T): T
| |||||
| |||||
Lower bound of a template parameter
callable<T super Some>(T): T
| |||||
Default of a template parameter
callable<T = int>(T): T
| |||||