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
General table across all type parsing capabilities
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
| |||
User[]
| |||
ExampleCollection<array-key, User>
| |||
| |||
ExampleCollection<in array-key, out User>
| |||
| |||
ExampleCollection<#[assert(not<"0">)] array-key>
| |||
|
| ||
ClassName::CONSTANT_NAME
| |||
Prefixed class constant mask types
ClassName::CONSTANT_*
| |||
Non-prefixed class constant mask types
ClassName::*
| |||
JSON_*
| |||
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 (
42
| |||
| |||
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
| |||
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
2e2
| |||
Float hexadecimal literals in scientific notation
0x42e2
| |||
Shape Types
Below is a list of grammar of shaped types.
object {
key: ValueType
}
| |||
|---|---|---|---|
| |||
Trailing comma in explicit shape types
object {
key: ValueType,
}
| |||
| |||
object {
ValueType
}
| |||
| |||
Trailing comma in implicit shape types
object {
ValueType,
}
| |||
Optional keys in explicit shape types
object {
key?: ValueType
}
| |||
| |||
object {}
| |||
| |||
object { ... }
| |||
| |||
object {
key: ValueType,
...
}
| |||
| |||
object {
ValueType,
...
}
| |||
| |||
object {
...<array-key, Type>
}
| |||
| |||
object {
#[inline, assert(not<"">)]
name: string
}
| |||
Callables Types
Below is a list of grammar of callable (function) types.
callable()
| |||
|---|---|---|---|
callable(): Type
| |||
Callable with typed parameters
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 prefixed variadic parameters
callable(...Type): T
| |||
Callable with prefixed and suffixed variadic parameters
callable(...Type ...$name): T
| |||
| |||
Callable with optional variadic parameters
callable(Type...=): T
callable(...Type=): T
| |||
| |||
Callable with named variadic parameters
callable(Type ...$name): T
callable(...Type $name): T
| |||
| |||
Callable with output named variadic parameters
callable(Type &...$name): T
callable(...Type &$name): T
| |||
| |||