Какие типы значения может вернуть функция инструкцией return php

Возврат значений

Значения возвращаются при помощи необязательного оператора возврата.
Возвращаемые значения могут быть любого типа, в том числе это могут
быть массивы и объекты. Возврат приводит к завершению выполнения функции и передаче
управления обратно к той строке кода, в которой данная функция была
вызвана. Для получения более детальной информации ознакомьтесь с описанием
return.

Замечание:

Если конструкция return не указана, то функция вернёт
значение null.

Использование выражения return

Пример #1 Использование конструкции return


<?php
function square($num)
{
return
$num * $num;
}
echo
square(4); // выводит '16'.
?>

Функция не может возвращать несколько значений, но аналогичного
результата можно добиться, возвращая массив.

Пример #2 Возврат нескольких значений в виде массива


<?php
function small_numbers()
{
return [
0, 1, 2];
}
// Деструктуризация массива будет собирать каждый элемент массива индивидуально
[$zero, $one, $two] = small_numbers();// До версии 7.1.0 единственной эквивалентной альтернативой было использование конструкции list().
list($zero, $one, $two) = small_numbers();?>

Для того, чтобы функция возвращала результат по ссылке, вам
необходимо использовать оператор & и при описании функции,
и при присвоении переменной возвращаемого значения:

Пример #3 Возврат результата по ссылке


<?php
function &returns_reference()
{
return
$someref;
}
$newref =& returns_reference();
?>

Для получения более детальной информации о ссылках обратитесь
к разделу документации Подробно о ссылках.

ryan dot jentzsch at gmail dot com

6 years ago


PHP 7.1 allows for void and null return types by preceding the type declaration with a ? -- (e.g. function canReturnNullorString(): ?string)

However resource is not allowed as a return type:

<?php
function fileOpen(string $fileName, string $mode): resource
{
   
$handle = fopen($fileName, $mode);
    if (
$handle !== false)
    {
        return
$handle;
    }
}
$resourceHandle = fileOpen("myfile.txt", "r");
?>

Errors with:
Fatal error: Uncaught TypeError: Return value of fileOpen() must be an instance of resource, resource returned.


rstaveley at seseit dot com

12 years ago


Developers with a C background may expect pass by reference semantics for arrays. It may be surprising that  pass by value is used for arrays just like scalars. Objects are implicitly passed by reference.

<?php# (1) Objects are always passed by reference and returned by referenceclass Obj {
    public
$x;
}

function

obj_inc_x($obj) {
   
$obj->x++;
    return
$obj;
}
$obj = new Obj();
$obj->x = 1;$obj2 = obj_inc_x($obj);
obj_inc_x($obj2);

print

$obj->x . ', ' . $obj2->x . "n";# (2) Scalars are not passed by reference or returned as suchfunction scalar_inc_x($x) {
   
$x++;
    return
$x;
}
$x = 1;$x2 = scalar_inc_x($x);
scalar_inc_x($x2);

print

$x . ', ' . $x2 . "n";# (3) You have to force pass by reference and return by reference on scalarsfunction &scalar_ref_inc_x(&$x) {
   
$x++;
    return
$x;
}
$x = 1;$x2 =& scalar_ref_inc_x($x);    # Need reference here as well as the function sig
scalar_ref_inc_x($x2);

print

$x . ', ' . $x2 . "n";# (4) Arrays use pass by value sematics just like scalarsfunction array_inc_x($array) {
   
$array{'x'}++;
    return
$array;
}
$array = array();
$array['x'] = 1;$array2 = array_inc_x($array);
array_inc_x($array2);

print

$array['x'] . ', ' . $array2['x'] . "n";# (5) You have to force pass by reference and return by reference on arraysfunction &array_ref_inc_x(&$array) {
   
$array{'x'}++;
    return
$array;
}
$array = array();
$array['x'] = 1;$array2 =& array_ref_inc_x($array); # Need reference here as well as the function sig
array_ref_inc_x($array2);

print

$array['x'] . ', ' . $array2['x'] . "n";


bgalloway at citycarshare dot org

15 years ago


Be careful about using "do this thing or die()" logic in your return lines.  It doesn't work as you'd expect:

<?php
function myfunc1() {
    return(
'thingy' or die('otherthingy'));
}
function
myfunc2() {
    return
'thingy' or die('otherthingy');
}
function
myfunc3() {
    return(
'thingy') or die('otherthingy');
}
function
myfunc4() {
    return
'thingy' or 'otherthingy';
}
function
myfunc5() {
   
$x = 'thingy' or 'otherthingy'; return $x;
}
echo
myfunc1(). "n". myfunc2(). "n". myfunc3(). "n". myfunc4(). "n". myfunc5(). "n";
?>

Only myfunc5() returns 'thingy' - the rest return 1.


k-gun !! mail

6 years ago


With 7.1, these are possible yet;

<?php
function ret_void(): void {
   
// do something but no return any value
    // if needs to break fn exec for any reason simply write return;
   
if (...) {
        return;
// break
        // return null; // even this NO!
   
}$db->doSomething();
   
// no need return call anymore
}

function

ret_nullable() ?int {
    if (...) {
        return
123;
    } else {
        return
null; // MUST!
   
}
}
?>


nick at itomic.com

19 years ago


Functions which return references, may return a NULL value. This is inconsistent with the fact that function parameters passed by reference can't be passed as NULL (or in fact anything which isnt a variable).

i.e.

<?phpfunction &testRet()
{
    return
NULL;
}

if (

testRet() === NULL)
{
    echo
"NULL";
}
?>

parses fine and echoes NULL


ryan dot jentzsch at gmail dot com

7 years ago


PHP 7 return types if specified can not return a null.
For example:
<?php
declare(strict_types=1);

function

add2ints(int $x, int $y):int
{
   
$z = $x + $y;
    if (
$z===0)
    {
        return
null;
    }
    return
$z;
}
$a = add2ints(3, 4);
echo
is_null($a) ? 'Null' : $a;
$b = add2ints(-2, 2);
echo
is_null($b) ? 'Null' : $b;
exit();
Output:
7
Process finished with
exit code 139

Berniev

5 years ago


Be careful when introducing return types to your code.

Only one return type can be specified (but prefacing with ? allows null).

Return values of a type different to that specified are silently converted with sometimes perplexing results. These can be tedious to find and will need rewriting, along with calling code.

Declare strict types using "declare(strict_types=1);" and an error will be generated, saving much head-scratching.


zored dot box at gmail dot com

5 years ago


You may specify child return type if there is no parent:

<?phpclass A {
    public function
f ($a)
    {
        return
1;
    }
}

class

B extends A {
    public function
f ($a): int // + return type, OK
   
{
        return
1;
    }
}

class

C extends A {
    public function
f (int $a) // + argument type, WARNING
   
{
        return
1;
    }
}
?>


Vidmantas Maskoliunas

7 years ago


Note: the function does not have "alternative syntax" as if/endif, while/endwhile, and colon (:) here is used to define returning type and not to mark where the block statement begins.

php(@)genjo(DOT)fr

3 years ago


Declaring a collection of objects as return type is not implemented and forbidden:
<?php
class Child{}

function

getChilds(): Child[]
{
    return [(new
Child()), (new Child())];
}
var_dump(getChilds());
// Returns:  Parse error: syntax error, unexpected '[', expecting '{'
?>

We have to use:
<?php
class Child{}

function

getChilds(): array
{
    return [(new
Child()), (new Child())];
}
var_dump(getChilds());
// Returns:
/*
array (size=2)
  0 =>
    object(Child)[168]
  1 =>
    object(Child)[398]
*/
?>

Idem for function parameter:
<?php
function setChilds(Child[] $childs){}
// Not allowedfunction setChilds(array $childs){}
// Allowed
?>

Значения возвращаются при помощи необязательного оператора возврата.
Возвращаемые значения могут быть любого типа, в том числе это могут
быть массивы и объекты. Возврат приводит к завершению выполнения функции и передаче
управления обратно к той строке кода, в которой данная функция была
вызвана. Для получения более детальной информации ознакомьтесь с описанием
return.

Замечание:

Если конструкция return не указана, то функция вернет
значение NULL.

Использование выражения return

Пример #1 Использование конструкции return


<?php
function square($num)
{
    return 
$num $num;
}
echo 
square(4);   // выводит '16'.
?>

Функция не может возвращать несколько значений, но аналогичного
результата можно добиться, возвращая массив.

Пример #2 Возврат нескольких значений в виде массива


<?php
function small_numbers()
{
    return array (
012);
}
list (
$zero$one$two) = small_numbers();
?>

Для того, чтобы функция возвращала результат по ссылке, вам
необходимо использовать оператор & и при описании функции,
и при присвоении переменной возвращаемого значения:

Пример #3 Возврат результата по ссылке


<?php
function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>

Для получения более детальной информации о ссылках обратитесь
к разделу документации Подробно о ссылках.

Объявление типов возвращаемых значений

В PHP 7 добавлена возможность объявлять тип возвращаемого значения. Аналогично
объявлению типов аргументов
можно задать тип значения, которое будет возвращаться функцией.
Типы, которые можно
объявить для возвращаемых значений те же, что и для аргументов фукнций.

Режим строгой типизации
также работает для объявлении типа возвращаемого значения. В обычном режиме слабой
типизации возвращаемое из функции значение приводится к корректному типу. При строгой
типизации возвращаемое значение должно быть заданного типа, иначе будет выброшено
исключение TypeError.

Замечание:

Если переопределяется родительский метод, возвращаемое значение дочернего метода должно
быть того же типа, что и родительского. Если в родительском методе не задан тип возвращаемого
значения, то и дочерний метод этот тип может не объявлять.

Примеры

Пример #4 Обычное объявление типа возвращаемого значения


<?php
function sum($a$b): float {
    return 
$a $b;
}
// Будет возвращаться значение типа float.
var_dump(sum(12));
?>

Результат выполнения данного примера:

Пример #5 То же в режиме строгой типизации


<?php
declare(strict_types=1);

function 

sum($a$b): int {
    return 
$a $b;
}
var_dump(sum(12));
var_dump(sum(12.5));
?>

Результат выполнения данного примера:

int(3)

Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned in - on line 5 in -:5
Stack trace:
#0 -(9): sum(1, 2.5)
#1 {main}
  thrown in - on line 5

Пример #6 Возврат объектов


<?php
class {}

function 

getC(): {
    return new 
C;
}
var_dump(getC());
?>

Результат выполнения данного примера:

Вернуться к: Функции

Karleb

Karleb

Posted on Mar 25

• Updated on Mar 27



 



 



 



 



 

 

Return types in PHP refer to the type of data returned by a function in PHP. In strongly typed languages like JAVA, every function must have a return type but it is optional in PHP as it is a loosely typed programming language. It is a good practice to include return types as it reduces ambiguity, limits error and improves code quality.

We will be looking into several return types in PHP. Without any further ado, let’s dive in.

Prerequisite

  • PHP version 8

  • Basic knowledge of PHP

To enforce return type in PHP functions, we need to include a simple declaration at the top of the file.

declare(strict_types=1);

Enter fullscreen mode

Exit fullscreen mode

This is a PHP directive that enforces strict typing. It is used to ensure that only values of the expected data type are passed to a function or method.

When strict_types is set to 1, PHP will require that function arguments are of the correct data type. If an argument is of the wrong type, PHP will throw a TypeError instead of attempting to convert the argument to the expected type.

With this in place, we can be rest assured PHP is not doing any type conversion that we may not be aware of when incorrect types are used.

The type of data we want the function or method to return is declared by putting a colon and the type between the closing parenthesis and the opening curly bracket.

int

int return type represents integers in PHP. They are positive or negative whole numbers without decimals, int can also be zero.


function addition(int $a, int $b): int {
  $sum  = $a + $b;
  return $sum;
}

echo addition(12, 20);

Enter fullscreen mode

Exit fullscreen mode

In class example:


class Arithmetic {
  public function __construct(public int $a, public  int $b) {}

  public function addition(): int {
    $sum = $this->a + $this->b;
    return $sum;
  }
}

echo (new Arithmetic(12, 20))->addition();

Enter fullscreen mode

Exit fullscreen mode

string

The string is a return type used to represent text. It can contain any sequence of characters, including letters, numbers, symbols, and whitespace.

Strings can be declared using a single or double quote.


function greeting(): string {
 return "Good day fellow!";
}

echo greeting();

Enter fullscreen mode

Exit fullscreen mode


class Greeting {
  public function __construct(public string $message) {}

  public function greeting(): string {
    return $this->message;
  }
}

echo (new Greeting("Good day fellow!"))->greeting();

Enter fullscreen mode

Exit fullscreen mode

float

Float is a return type used to represent numbers with a decimal point. Floats are also known as «floating point numbers» or «doubles».


function price(): float {
  return 10.56;
}

echo price();

Enter fullscreen mode

Exit fullscreen mode


class Socks {
  public function price(): float {
    return 10.56;
  }
}

(new Socks())->price();

Enter fullscreen mode

Exit fullscreen mode

Floats can be negatives too.


function price(): float {
  return -1.78;
}

echo price();

Enter fullscreen mode

Exit fullscreen mode


class Socks {
  public function price(): float {
    return -1.78;
  }
}

(new Socks())->price();

Enter fullscreen mode

Exit fullscreen mode

bool

bool, short for «boolean», is a return type used to represent logical values. A bool variable can only have two possible values: true or false.

It is important to note that PHP returns 1 for true and nothing for false.


function isComplete(): bool {
  return true;
}

echo isComplete();


function isAdult(int $age): bool {
  return $age >= 18;
}

echo isAdult(10);

Enter fullscreen mode

Exit fullscreen mode


class Task {
  public function isComplete(): bool {
    return true;
  }
}

class Person {
   public function isAdult(int $age): bool {
    return $age >= 18;
  }
}

echo (new Task)->isComplete();
echo (new Person)->isAdult(2);

Enter fullscreen mode

Exit fullscreen mode

array

The array is a return used when a method or function is expected to return an array. PHP has index, associative and multidimensional arrays, any of these can be returned when an array return value is used.


function playlist(): array {
  return ['Diced Pineapple', 'Formation', 'Thinking Out Loud'];
}

print_r(playlist());

function user(): array {
  return ['name' => 'John', 'age' => 25, 'location' => 'New York'];
}

print_r(user());

Enter fullscreen mode

Exit fullscreen mode


class Playlist {
  public function music(): array {
    return ['Diced Pineapple', 'Formation', 'Thinking Out Loud'];
  }

  public function user(): array {
    return ['name' => 'John', 'age' => 25, 'location' => 'New York'];
  }
}

print_r(
  (new Playlist)->music()
);

print_r(
  (new Playlist)->user()
);

Enter fullscreen mode

Exit fullscreen mode

void

In the case we do not wish to return anything, the void is the goto return type. It simply means nothing will be returned.


function earth(): void {
  $title = "This planet supports life";
  $age_in_years = 4_800_000_000;
}

earth();

Enter fullscreen mode

Exit fullscreen mode


class Planet {
  public function earth(): void {
    $title = "This planet supports life";
    $age_in_years = 4_800_000_000;
  }
}

(new Planet)->earth();

Enter fullscreen mode

Exit fullscreen mode

mixed

A mixed return type is used to indicate that a function or method may return different data types, depending on the input or the logic within the function. This is helpful when you cannot determine the exact return type ahead of time, or when the return type is dependent on a complex set of conditions.


function randomType(): mixed {

    $value;

    switch(rand(1, 5)) {

      case 1: $value = "Good day fellow!";
      break;
      case 2: $value = 10.22;
      break;
      case 3: $value = true;
      break;
      case 4: $value = ['Sport', 'Movies'];
      break;
      default: $value = 100;
      break;

    }

    return $value;
}

echo randomType();

Enter fullscreen mode

Exit fullscreen mode


class RandomType {
  public function value(): mixed {

    $value;

    switch(rand(1, 5)) {

      case 1: $value = "Good day fellow!";
      break;
      case 2: $value = 10.22;
      break;
      case 3: $value = true;
      break;
      case 4: $value = ['Sport', 'Movies'];
      break;
      default: $value = 100;
      break;

    }

    return $value;

  }
}


echo (new Randomtype)->value();

Enter fullscreen mode

Exit fullscreen mode

callable

The callable return type in PHP allows a function to indicate that it returns a callable or a function reference that can be called later.


function getMultiplier(int $factor): callable {
    return function (int $number) use ($factor) {
        return $number * $factor;
    };
}

$multiplyByTwo = getMultiplier(2);
echo $multiplyByTwo(5); 


$multiplyByThree = getMultiplier(3);
echo $multiplyByThree(6);

Enter fullscreen mode

Exit fullscreen mode


class Multiplier {

    public function __construct(public int $factor) {}

    public function getMultiplier(): callable {
        return function (int $number) {
            return $number * $this->factor;
        };
    }
}

$multiplyByTwo = (new Multiplier(2))->getMultiplier();
echo $multiplyByTwo(5);

$multiplyByThree = (new Multiplier(3))->getMultiplier();
echo $multiplyByThree(6);

Enter fullscreen mode

Exit fullscreen mode

resource

The resource return type is used to indicate that a function or method returns a resource type. Resource type is a special data type that represents an external resource, such as a file, a database connection or a network socket.


function openFile(string $filename): resource {

  $file = fopen($filename, "r");

  if (!$file) {
    throw new Exception("Failed to open file: $filename");
  }

  return $file;

}

Enter fullscreen mode

Exit fullscreen mode



class FileHandler {

  public function openFile(string $filename): resource {

    $file = fopen($filename, "r");

    if (!$file) {

      throw new Exception("Failed to open file: $filename");

    }

    return $file;

  }

}

Enter fullscreen mode

Exit fullscreen mode

class

Classes can also be a return type. In this example, the Zoo class has a method called getAnimal() that returns an object of the Animal class. The return type of this method is specified as Animal using the :Animal syntax after the method signature. This means the return type will be a type of Animal class or any of its subclasses.


class Animal {
  public function __construct(public string $name, public int $age) {}
}

function getAnimal(): Animal {
    return new Animal("Leo", 5);
}

print_r(getAnimal());

Enter fullscreen mode

Exit fullscreen mode


class Animal {
  public function __construct(public string $name, public int $age) {}
}

class Zoo {
  public function getAnimal(): Animal {
    return new Animal("Leo", 5);
  }
}

print_r((new Zoo())->getAnimal());

Enter fullscreen mode

Exit fullscreen mode

object

The object return type is similar to the :class return type but the major difference is, it will return instances of any class and not a particular class.


class Address {
  public function __construct(public string $street, public string $city, public string $state) {}
}

class Person {
  public function __construct(public string $name, public Address $address) {}

  public function getAddress(): object {
    return $this->address;
  }
}

$address = new Address("123 Main St", "Anytown", "CA");
$person = new Person("John Doe", $address);
$addressObj = $person->getAddress();

Enter fullscreen mode

Exit fullscreen mode

Note that since the return type of getAddress() is specified as :object, the returned value can be an instance of any class, not just Address.

union

What if we can not tell the actual type a class should return or do we want to intentionally make the return type for any class dynamic?

PHP allows us to include additional return types using the union type, this allows us to include alternate types by separating them with a pipe |.


function price(): int | float {
  return 10;
}

function price(): int | float {
  return 10.4;
}

Enter fullscreen mode

Exit fullscreen mode


class Socks {
  public function price(): int | float {
    return 10;
  }
}

(new Socks)->price();

class Socks {
  public function price(): int | float {
    return 10.4;
  }
}

(new Socks)->price();

Enter fullscreen mode

Exit fullscreen mode

The optional type can have as many as possible types separated by a pipe, this feature should not be abused.

What If We Want a Null Value Returned

To return a null type, we need to add ? in front of the return type.


function hello(): ?string {
  return null;
}

hello();

Enter fullscreen mode

Exit fullscreen mode

In this case, the return type of the function can be either a string or null. No exception will be thrown.


class Optional {

  public function hello(): ?string {
    return null;
  }

  public function number(): ?int {
    return null;
  }

  public function price(): ?float {
    return null;
  }
}

echo (new Optional)->hello();
echo (new Optional)->number();
echo (new Optional)->price();

Enter fullscreen mode

Exit fullscreen mode

Conclusion

Return types are an important feature in PHP that allows developers to specify the type of value that a function should return. By declaring a return type, PHP can ensure that the value returned by a function is of the expected type, which can help prevent errors and improve code quality.

In PHP 7 and later versions, developers can use several different types of return types, including scalar types like int, float, and string, as well as compound types like array and callable. Additionally, PHP 8 introduced new return types, including union types and mixed types, which provide even more flexibility for developers.

Type declarations in PHP can be used not only for return types but also for parameter types and class properties. Type declarations can help prevent type errors, improve code readability, and make code easier to maintain.

Время на прочтение
5 мин

Количество просмотров 42K

Каждый мажорный релиз PHP добавляет ряд новых возможностей, некоторые из которых действительно имеют значение. Для PHP 5.3 — это были пространства имен и анонимные функции. Для PHP 5.4 — трейты. Для PHP 5.5 — генераторы. Для 5.6 — списки аргументов переменной длины.

PHP 7 имеет большое количество новшеств и улучшений, делающих жизнь разработчика легче. Но я считаю, что самым важным и долгосрочным изменением является работа с типами. Совокупность новых фич изменит взгляд на PHP разработку в лучшую сторону.

Почему поддержка строгой типизации так важна? Она предоставляет программе — компилятору или рантайму и другим разработчикам ценную информацию о том, что вы пытались сделать, без необходимости исполнять код. Это дает три типа преимуществ:

  1. Становится намного легче сообщать другим разработчикам цель кода. Это практически как документация, только лучше!
  2. Строгая типизация дает коду узкую направленность поведения, что способствует повышению изоляции.
  3. Программа читает и понимает строгую типизацию точно также как человек, появляется возможность анализировать код и находить ошибки за вас… прежде чем вы его исполните!

В подавляющем большинстве случаев, явно типизированный код будет легче понять, он лучше структурирован и имеет меньше ошибок, чем слабо типизированный. Только в небольшом проценте случаев строгая типизация доставляет больше проблем, чем пользы, но не беспокойтесь, в PHP проверка типов все еще является опциональной, также как и раньше. Но на самом деле, ее стоит использовать всегда.

Возвращаемые типы

Первым дополнением к уже существующей системе типизации будет поддержка возвращаемых типов. Теперь можно указывать в функциях и методах тип возвращаемого значения в явном виде. Рассмотрим следующий пример:

class Address {

  protected $street;
  protected $city;
  protected $state;
  protected $zip;

  public function __construct($street, $city, $state, $zip) {
    $this->street = $street;
    $this->city = $city;
    $this->state = $state;
    $this->zip = $zip;
  }

  public function getStreet() { return $this->street; }
  public function getCity() { return $this->city; }
  public function getState() { return $this->state; }
  public function getZip() { return $this->zip; }

}

class Employee {
  protected $address;

  public function __construct(Address $address) {
    $this->address = $address;
  }

  public function getAddress() : Address {
    return $this->address;
  }
}

$a = new Address('123 Main St.', 'Chicago', 'IL', '60614');
$e = new Employee($a);

print $e->getAddress()->getStreet() . PHP_EOL;
// Prints 123 Main St.

В этом довольно приземленном примере у нас есть объект Employee, который имеет только одно свойство, содержащее переданный нами почтовый адрес. Обратите внимание на метод getAddress(). После параметров функции у нас есть двоеточие и тип. Он является единственным типом, который может принимать возвращаемое значение.

Постфиксный синтаксис для возвращаемых типов может показаться странным для разработчиков, привыкших к C/C++ или Java. Однако, на практике подход с префиксным объявлением не подходит для PHP, т.к. перед именем функции может идти множество ключевых слов. Во избежание проблем с парсером PHP выбрал путь схожий с Go, Rust и Scala.

При возврате любого другого типа методом getAddress() PHP будет выбрасывать исключение TypeError. Даже null не будет удовлетворять требованиям типа. Это позволяет нам с абсолютной уверенностью обращаться в print к методом объекта Address. Мы точно будем знать, что действительно вернется объект именно этого типа, не null, не false, не строка или какой-то другой объект. Именно этим обеспечивается безопасность работы и отсутствие необходимости в дополнительных проверках, что в свою очередь делает наш собственный код чище. Даже если что-то пойдет не так, PHP обязательно предупредит нас.

Но что делать, если у нас менее тривиальный случай и необходимо обрабатывать ситуации, когда нет объекта Address? Введем EmployeeRepository, логика которого позволяет не иметь записей. Сначала мы добавим классу Employee поле ID:

class Employee {

    protected $id;
    protected $address;

    public function __construct($id, Address $address) {

        $this->id = $id;
        $this->address = $address;

    }

    public function getAddress() : Address {
        return $this->address;
    }

}

А теперь создадим наш репозиторий. (В качестве заглушки добавим фиктивные данные прямо в конструктор, но на практике, конечно же, необходимо иметь источник данных для нашего репозитория).

class EmployeeRepository {

    private $data = [];
    public function __construct() {

        $this->data[123] = new Employee(123, new Address('123 Main St.', 'Chicago', 'IL', '60614'));
        $this->data[456] = new Employee(456, new Address('45 Hull St', 'Boston', 'MA', '02113'));

    }

    public function findById($id) : Employee {
        return $this->data[$id];
    }

}

$r = new EmployeeRepository();

print $r->findById(123)->getAddress()->getStreet() . PHP_EOL;

Большинство читателей быстро заметит, что `findById()` имеет баг, т.к. в случае, если мы попросим несуществующий идентификатор сотрудника PHP будет возвращать `null` и наш вызов `getAddress()` умрет с ошибкой «method called on non-object». Но на самом деле ошибка не там. Она заключается в том, что `findById()` должен возвращать сотрудника. Мы указываем возвращаемый тип `Employee`, чтобы было ясно чья это ошибка.

Что же делать, если действительно нет такого сотрудника? Есть два варианта: первый — исключение; если мы не можем вернуть то, что мы обещаем — это повод для особой обработки за пределами нормального течения кода. Другой — указание интерфейса, имплементация которого и будет возвращена (в том числе и «пустая»). Таким образом, оставшаяся часть кода будет работать и мы сможем контролировать происходящее в «пустых» случаях.

Выбор подхода зависит от варианта использования и также определяется рамками недопустимости последствий в случае невозвращения правильного типа. В случае репозитория, я бы поспорил с выбором в пользу исключений, поскольку шансы попасть именно в такую ситуацию минимальны, а работа через исключения является довольно дорогостоящей по производительности. Если бы мы имели дело со скалярной переменной, то обработка «пустого значения» было бы приемлемым выбором. Модифицируем наш код соответственно (для краткости показаны только измененные части):

interface AddressInterface {

    public function getStreet();
    public function getCity();
    public function getState();
    public function getZip();

}

class EmptyAddress implements AddressInterface {

    public function getStreet() { return ''; }
    public function getCity() { return ''; }
    public function getState() { return ''; }
    public function getZip() { return ''; }

}

class Address implements AddressInterface {

    // ...

}

class Employee {

    // ...

    public function getAddress() : AddressInterface {

        return $this->address;

    }

}

class EmployeeRepository {

    // ...

    public function findById($id) : Employee {

        if (!isset($this->data[$id])) {
            throw new InvalidArgumentException('No such Employee: ' . $id);
        }
        return $this->data[$id];
    }

}

try {
    print $r->findById(123)->getAddress()->getStreet() . PHP_EOL;
    print $r->findById(789)->getAddress()->getStreet() . PHP_EOL;
} catch (InvalidArgumentException $e) {
    print $e->getMessage() . PHP_EOL;
}

/* 
 * Prints:
 * 123 Main St.
 * No such Employee: 789
 */

Теперь getStreet() будет отдавать хорошее пустое значение.

Одно важное примечание о возвращаемых типах: при наследовании тип не может быть изменен, даже нельзя сделать его более конкретным (подклассом, например). Причина в особенностях ленивой загрузки PHP.

Возвращаемые типы являются большой, но далеко не единственной новой особенностью, расширяющей систему типов PHP. Во второй части мы рассмотрим другое, пожалуй даже более важное изменение: декларирование скалярных типов.

Последнее обновление: 11.03.2021

Функция может возвращать некоторое значение — число, строку и т.д., то есть некоторый результат.
Для возвращения значения в функции применяется оператор return, после которого указывается возвращаемое значение.

Например, получим из функции сумму двух чисел:

<?php
function add($a, $b)
{
    return $a + $b;
}

$result = add(5, 6);
echo $result;			// 11
?>

Функция add() принимает два параметра и возвращает сумму их значений.

return $a + $b;

Поскольку функция возвращает значение, мы его можем присвоить переменной:

$result = add(5, 6);

Либо использовать напрямую:

echo add(4, 8);

Если после инструкции return в функции идут другие инструкции, то они не будут выполняться:

function add($a, $b)
{
	$sum = $a + $b;
    return $sum;		// завершение функции
	echo "sum = $sum";	// эта строка не будут выполняться
}

В реальности даже если функция не использует оператор return, она все равно возвращает значение, только в этом случае это значение — null:

<?php
function add($a, $b)
{
	$sum = $a + $b;
	echo "sum = $sum<br />";
}

$result = add(5, 6);

if($result === null)
	echo "result равен null";
else
	echo "result не равен null";
?>

Понравилась статья? Поделить с друзьями:
  • War tales руководство
  • Aroma hair care provita ampoule инструкция по применению
  • Multifunctional water pipe инструкция на русском
  • Контрамутан инструкция по применению на русском капли
  • Ибупрофен инструкция по применению сироп для детей 7 лет дозировка