Return Type Declarations in PHP

PHP 7 now makes it possible to declare return types for your methods. This allows you better control over the data that will be returned from each method in your application.

The types of data you can return include but are not limited to the following.

self

array

callable

bool

float

int

string

Apart from the above, you can also specify a type of Object that should be returned by passing the name of the Class or Interface.

Example

First let’s specify the return type for our Calculator class. We do this by specifying the return type after the argument definition by appending a colon “:”, followed by the return type.

1

2

3

4

5

6

7

8

9

10

<?php

classCalculator

{

publicfunctionsum(float...$numbers):float

{

returnarray_sum($numbers);

}

}

This will require the method sum to return the value of a float. If for some reason a different value is returned then you’d get a TypeError exception thrown.

Let’s look at another example that specifies a type of object expected as the return value when requesting the name of a person.

We’ll start by creating a Person class, that relies on a PersonName class for the person’s name.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<?php

classPerson

{

private$firstName;

private$lastName;

private$age;

publicfunction__construct(string$firstName,string$lastName,int$age)

{

$this->firstName=$firstName;

$this->lastName=$lastName;

$this->age=$age;

}

publicfunctionname():PersonName

{

returnnewPersonName($this->firstName,$this->lastName);

}

}

classPersonName

{

private$firstName;

private$lastName;

publicfunction__construct(string$firstName,string$lastName)

{

$this->firstName=$firstName;

$this->lastName=$lastName;

}

publicfunctionfull():string

{

return$this->first().' '.$this->last();

}

publicfunctionfirst():string

{

return$this->firstName;

}

publicfunctionlast():string

{

return$this->lastName;

}

}

Strict Typing

Another option you have is to declare strict typing for your classes, which takes you from the default weak mode into a strong mode the forces the correct value to be returned rather then being coerced into the correct value. When the correct value type is not returned or passed, a TypeError exception will be thrown.

Let’s look at an example using strict mode.

1

2

3

4

5

6

7

8

9

10

11

12

<?php

declare(strict_types=1);

classCalculator

{

publicfunctionsum(float...$numbers):float

{

returnarray_sum($numbers);

}

}

You’ll see that we defined it by specifying the document in strict mode at the top. Now when we use it the values returned and passed will have to be either a float or a int, if not a TypeError is thrown.

Now let’s change it up a little and require int’s as the arguments and a int as the return value.

1

2

3

4

5

6

7

8

9

10

11

12

<?php

declare(strict_types=1);

classCalculator

{

publicfunctionsum(int...$numbers):int

{

returnarray_sum($numbers);

}

}

If a float is passed as an argument or returned as a return value, then a TypeError exception will be thrown since a float is not the same as a integer.

Previously you could pass your numbers as a string to a method as well, and PHP would convert it to the correct type prior to processing it. Now with strict typing enabled, the TypeError exception would be thrown in those cases as well.

Tags

About This Blog

I'm writing this blog in order to share what I'm learning with the world. Since you learn best by teaching others and writing things down, I figured this would be the best way to really tackle any new technologies I plan on learning. I hope this blog will help you as much as it will benefit me.