Tutorial by Examples

Support for type hinting array parameters (and return values after PHP 7.1) was added in PHP 5.1 with the keyword array. Any arrays of any dimensions and types, as well as empty arrays, are valid values. Support for type hinting callables was added in PHP 5.4. Any value that is_callable() is valid ...
Since PHP objects don't inherit from any base class (including stdClass), there is no support for type hinting a generic object type. For example, the below will not work. <?php function doSomething(object $obj) { return $obj; } class ClassOne {} class ClassTwo {} $classOne= new...
Type hinting for classes and interfaces was added in PHP 5. Class type hint <?php class Student { public $name = 'Chris'; } class School { public $name = 'University of Edinburgh'; } function enroll(Student $student, School $school) { echo $student->name . ' is b...
In PHP 7.1, the void return type was added. While PHP has no actual void value, it is generally understood across programming languages that a function that returns nothing is returning void. This should not be confused with returning null, as null is a value that can be returned. function lacks_re...
Parameters Nullable type hint was added in PHP 7.1 using the ? operator before the type hint. function f(?string $a) {} function g(string $a) {} f(null); // valid g(null); // TypeError: Argument 1 passed to g() must be of the type string, null given Before PHP 7.1, if a parameter has a typ...

Page 1 of 1