I studied Java
in my university. It was strictly typed programming language and had to define the
return type for a function.
But, I started my career as PHP developer with PHP programming.
PHP 5.6
did not had a concept of return type or type hinting.
In my early days, I used to get confused.
I worked in many small to big projects. For all projects I used PHP and this language become primary backend programming language for me.
Now, I am also working with other languages. But, I have deep connection with PHP. I am closely monitoring the evolution in PHP space.
PHP 7 followed on the heels of PHP 5.6 and the release was a big revolution.
Chronologically PHP 7 is confusing version but brought enormous improvements in PHP engine performance. It also introduced a variety of new, impactful features that made it quickly adopted.
In this release PHP community added a new impactful feature Type hinting. Type hinting is being referred to as Return Type declaration. Return type declaration specifies the type of value that a function should return. Also, community releasing new Type Hinting in new releases.
In this post, I will try to explain Type hinting added in each release with example.
PHP 7.0.0
PHP 7.0.0
added new features to support int
float
, bool
, string
, interfaces
, array
and callable
return types.
I will explain them with examples.
1. int
return type
int
return type declaration specifies that a function can return integer value.
Example:
2. float
return type
float
return type declaration specifies that a function can return floating number ie 1.2
, 0.5
, etc.
Example:
3. bool
return type
bool
return type declaration specifies that a function should return boolean value fo true
or false
Example:
4. string
return type
string
return type declaration specifies that a function should return boolean value of string. It could be combination of alphabets, numbers and special characters.
Example:
5. interfaces
return type
interfaces
return type declaration specifies that a function should return interfaces rather than a specific value.
Example:
|
|
6. array
return type
array
return type declaration specifies that a function should return array value.
Example:
7. callable
return type
Type declarations can be added to function arguments, return values.
PHP 7.1.0
PHP 7.1.0
added new features to support iterable
void
, and null
return types.
1. iterable
return type
iterable
is a built-in compile time type alias for array|Traversable.
Example:
2. void
return type
void
return type is used in a function if function dose not return anything.
Example
3. nullable
return type
null
can be passed as an argument, or returned as a value, respectively.
Example:
PHP 7.2.0
PHP 7.2.0
added new features to support object
return type for a function.
1. object
Return type
Object
is used to return an instances of any class. In our example, we are declaring Object
as return type for returnA()
in class B
|
|
PHP 8.0.0
PHP 8.0.0
added new features to support Union return type
, only static
and mixed
return type for a function.
1. Union return type
PHP 7.1.0
have nullable
types, which means you can declare the type to be null
with a type declaration similar to ?string
From PHP 8.0
, you can declare more than one type for arguments, return types, and class properties.
Example:
2. return only type static
PHP class methods can return self
and parent in older versions, but static
was not allowed.
PHP 8.0
allowed static
return type.
Example
3. mixed
return type
mixed
return type is equivalent to a Union Type of:
string|int|float|bool|null|array|object|callable|resource
Example
PHP 8.1.0
PHP 8.1.0
added new features to support never
and intersection
types.
Also, returning by reference from a void function was deprecated
1. never
return type
The function that always throws an exception or terminates with a die/exit call can be declared with the never
.
A never
return type indicates that it will never return a value,
It’s similar to the void
return type, but the never
return type guarantees that the program will throw and exception or terminate.
Example
2. intersection
types
intersection
types let you typehint values that must satisfy more than one type constraint.
From PHP 8.1.0
we already have union types that combine types with a logical “or”
clause; intersection
types offer an logical “and”
clause instead.
We already know that “the type of $value
should be A
or B
”:
As we discussed above intersection
types offer an logical “and”
clause. The $value
type should be A
and B
:
This type hint is not frequently used like other type hints.
PHP 8.2.0
PHP 8.2.0
added new features to support literal type true
, DNF
types and null
and false
can be used standalone.
Example
1. The types null
and false
can be used standalone.
PHP 8.2.0 started to support null
and false
as standalone types.
In previous, versions, we can not use these types like standalone type.
Example
But can not do like this. This will throw error
|
|
2. literal type true
PHP 8.2.0 started to support true
types, DNF
types and
true
type can be used as part of Union Types
, But you can not use it with bool
it creates ambiguity
3. DNF
types
PHP 8.2.0
started to support DNF
type by combining Union Types available form PHP 8.0
and Intersection Types available form PHP 8.1
.
One of the most common use cases is to declare a type that accepts an Intersection type, or null (i.e. nullable intersection types).
which means they can become nullable: (A&B)|null
Example