| Start |
|---|
| Type hinting scalar types, arrays and callables Support for type hinting array parameters (and return values after PHP 7.1) was added in PHP 5.1 with the keyword Support for type hinting callables was added in PHP 5.4. Any value that If a typo occurs in the function name such that it is not Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type callable, string/array given Nonstatic methods can also be passed as callables in static format, resulting in a deprecation warning and level E_STRICT error in PHP 7 and 5 respectively. Method visibility is taken into account. If the context of the method with the Output: Fatal error: Uncaught TypeError: Argument 1 passed to r() must be callable, array given Support for type hinting scalar types was added in PHP 7. This means that we gain type hinting support for By default, PHP will attempt to cast any provided argument to match its type hint. Changing the call to To stop this behavior, one must add The above script now produces a fatal error: Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, float given An Exception: Special TypesSome PHP functions may return a value of type As an example, TypeError: Argument 1 passed to sample() must be an instance of resource, resource given |
| Type hinting classes and interfaces Type hinting for classes and interfaces was added in PHP 5. Class type hintThe above script outputs: Chris is being enrolled at University of Edinburgh Interface type hintThe above example outputs the same as before: Chris is being enrolled at University of Edinburgh Self type hintsThe |
Nullable type hints ParametersNullable type hint was added in PHP 7.1 using the Before PHP 7.1, if a parameter has a type hint, it must declare a default value Return valuesIn PHP 7.0, functions with a return type must not return null. In PHP 7.1, functions can declare a nullable return type hint. However, the function must still return null, not void (no/empty return statements). |
| Type hinting generic objects Since PHP objects don't inherit from any base class (including For example, the below will not work. And will throw a fatal error: Fatal error: Uncaught TypeError: Argument 1 passed to doSomething() must be an instance of object, instance of OperationOne given A workaround to this is to declare a degenerate interface that defines no methods, and have all of your objects implement this interface. |
| Type Hinting No Return(Void) In PHP 7.1, the Note that if you declare a However, using return to exit the function is valid: Syntax
RemarksType hinting or type declarations (opens new window) are a defensive programming practice that ensures a function's parameters are of a specified type. This is particularly useful when type hinting for an interface because it allows the function to guarantee that a provided parameter will have the same methods as are required in the interface. Passing the incorrect type to a type hinted function will lead to a fatal error: Fatal error: Uncaught TypeError: Argument **X** passed to **foo()** must be of the type **RequiredType**, **ProvidedType** given |
Read Article
https://develop.horje.com/learn/1434/reference