Object types#
Unnamed objects#
object are examples of unnamed object types. This type is also a valid type in PHP.
Named objects#
stdClass, Foo, Bar\Baz etc. are examples of named object types. These types are also valid types in PHP.
Object properties#
Psalm supports specifying the properties of an object and their expected types, e.g.:
/** @param object{foo: string} $obj */
function takesObject(object $obj) : string {
return $obj->foo;
}
takesObject((object) ["foo" => "hello"]);
Optional properties can be denoted by a trailing ?, e.g.:
/** @param object{optional?: string} */
Generic object types#
Psalm supports using generic object types like ArrayObject<int, string>. Any generic object should be typehinted with appropriate @template tags.
Generators#
Generator types support up to four parameters, e.g. Generator<int, string, mixed, void>:
TKey, the type of theyieldkey - default:mixedTValue, the type of theyieldvalue - default:mixedTSend, the type of thesend()method's parameter - default:mixedTReturn, the return type of thegetReturn()method - default:mixed
Generator<int> is a shorthand for Generator<mixed, int, mixed, mixed>.
Generator<int, string> is a shorthand for Generator<int, string, mixed, mixed>.