Syntax
Names
A type name is written the way PHP writes an
identifier,
with one difference: a dash (-) may stand inside a name.
- The first character is a letter or an underscore (
_). - The rest are letters, digits, underscores and dashes, a dash cannot be the last character.
- A letter is
a-z,A-Zor a byte from0x80to0xff. - A dash is allowed only in a name without a namespace:
non-empty-stringis a name,Some\non-empty-stringandnon-empty\Someare not.
The words true, false and null in any case are
literals, not names, when they stand alone.
ExampleTypeName
resultTypeLang\Type\NamedTypeNode
A dash inside a name.
example-type
resultTypeLang\Type\NamedTypeNode
A reserved word as a part of a name.
true-type
resultTypeLang\Type\NamedTypeNode
A reserved word alone is a literal, not a name.
TrUe
resultTypeLang\Type\Literal\BoolLiteralNode
A name cannot begin with a digit.
42type
errorUnexpectedTokenException Unexpected "type"
A name cannot end with a dash.
example-
errorUnrecognizedSyntaxException Unrecognized syntax "-"
Namespace
Section titled “Namespace”A name may carry a namespace, separated with a backslash (\) the
way PHP does it.
A backslash may stand inside a name or at its start, not at its end. With a
namespace, the reserved words are ordinary names.
A relative name.
Example\Name
resultTypeLang\Type\NamedTypeNode
A fully qualified name.
\Absolute\Type\Name
resultTypeLang\Type\NamedTypeNode
The reserved words as a namespace and a name, not literals.
true\null
resultTypeLang\Type\NamedTypeNode
A name cannot end with a backslash.
example\name\
errorUnrecognizedSyntaxException Unrecognized syntax "\name\"
The $this type
Section titled “The $this type”The $this variable denotes the type of the object the type is written on. It
is a type of its own, so it stands wherever a type stands: inside a union,
behind a question mark, or as the type a
callable returns.
The type of the current object.
$this
resultTypeLang\Type\ThisTypeNode
The current object type, or nothing at all.
?$this
resultTypeLang\Type\WrappingTypeNode\NullableTypeNode
A callable returning the object it was called on.
callable(): $this
resultTypeLang\Type\Callable\CallableTypeNode
Any other variable is not a type.
$var
errorUnexpectedTokenException Unexpected end of input, expected "is not", "is"