Какие типы значения может вернуть функция инструкцией 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 мин

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

Планируемая дата выпуска PHP7 стремительно приближается, внутренняя группа усиленно работает, пытаясь исправить наш любимый язык, сделать его как можно лучше, будут удалены артефакты прошлых версий и добавлено несколько столь желанных фич. Есть много RFC, которые можно изучить и обсудить, но в этом посте я хотел бы сосредоточиться на трех самых важных.

PHP 5.7 vs. PHP7

Как я уже говорил в прошлом письме, 5.7 был отклонен в пользу перехода непосредственно к PHP7. Это означает, что не будет новой версии между 5.6 и 7 — даже если она и появилась бы, то просто служила бы сигналом тем, кто все еще погряз в устаревшем коде. Первоначально, 5.7 не должна была иметь новые функции, но должна была выбросить уведомления и предупреждения об устаревании кода, который скоро изменится в v7.

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

Обратите внимание, что пока голосование неоднозначно, это говорит о том, что идея окончательно не похоронена. Участники высказываются против 5.7 из-за отсутствия значительных изменений, но с учетом новых голосов на других RFC, они вполне могут изменить свое решение. Давайте посмотрим что произойдет.

Типы возвращаемого значения (Return Types)

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

function foo(): array {
    return [];
}

Улучшение? Однозначно!!! Но идеальное ли? К сожалению, нет:

  • возвращаемыми могут быть лишь те типы, которые есть на данный момент
    не будет возможности вернуть скалярные значения, такие как string, int, bool и др. Это означает, что ваши методы и функции, которые возвращают подобные значения по-прежнему останутся в исходном состоянии.
    Вы можете это исправить путем возврата экземпляров обертки для подобных значений, но это будет ненужным извращением в большинстве случаев. (прим. пер. — самое время обратить внимание на одно из предыдущих начинаний — преобразование примитивов в нормальные объекты)
  • нельзя будет определить возврат нескольких типов. Если ваша функция возвращает либо массив либо объект Iterator, то нет никакого способа указать это, например, array|Iterator как мы делаем в doc-блоках.

Некоторые люди также жаловались на объявления типа после закрывающей скобки списка аргументов, а не перед именем функции, но для меня это придирки. Популярные языки, такие как современный C++, используют “пост”-синтаксис, при этом сохраняется возможность поиска «function foo» без какой-либо необходимости прибегать к regex-модификациям. Более того, это согласуется с тем, что использует HHVM, таким образом получается непредвиденный бонус в совместимости.

Другие жаловались на «строгость» PHP, но, как заявил один из комментаторов, вы действительно должны знать что хотите получить еще до начала кодирования, через интерфейсы и наследование чужого кода. Кроме того, пока указание возвращаемых типов необязательно, и его наличие никак не влияет на общую производительность или стабильность PHP, не нужно раздувать из этого проблему. Жаловаться на такую фичу, все равно что жаловаться на наличие ООП в PHP в те времена, когда процедурные спагетти были хорошим способом решить задачу. Языки эволюционируют, и это один из шагов в правильном направлении.

А что думаете вы?

Удаление артефактов

Предстоящая версия предлагает удалить стилистические конструкции PHP4 (еще не голосовали). Вы можете прочитать и разобраться в этом RFC, это просто, и было бы бесполезно повторять еще раз — язык идет вперед. Мне не понятно почему такой шаг вызывает СТОЛЬКО душевных мук у некоторых людей. Например, тут: «Пожалуйста, не разрушайте наш язык», — просит Тони, который, похоже, с определенным умыслом использует удаляемый функционал.

Пост очень хорошо написан и, несмотря на очевидный гнев, заставляет меня задаться вопросом — если у вас такая кодовая база живет настолько долго, то стоит ли вообще обновляться до PHP7? И если уж настолько хочется, то стоит ли тащить все это с собой, разве не проще выявить поломанные классы и исправить их конструкторы? Вы даже можете делегировать эту задачу джуниорам, при условии достаточного покрытия кода тестами вы всегда сможете убедиться, что ничего не сломалось. И даже если тестов нет, если ваше приложение — это кусок кода непонятного качества, то вы действительно надеетесь получить выгоду от переезда на PHP7? Не лучше ли обновить ваше приложение в первую очередь?

Приговор «код, который я написал 10 лет назад, по-прежнему должен запускаться сегодня и должна быть возможность выполнить его через 10 лет» — для меня безумие — вы, безусловно, не должны ожидать подобного ни от одного популярного языка в рамках перехода от одной основной его версии к другой. Проведите параллель с реальным миром, вы не должны ожидать разрешения иметь рабов сегодня, только потому, что когда-то давно закон это разрешал. Да, был кровавый переход, но когда большинство сторонников рабства либо покаялись либо умерли, настал мир.

Стоит отдать должное Тони, он прав в том, что отстаивает свое мнение и прилагает большое количество усилий, чтобы его твердое «нет» было услышано. Но в долгосрочной перспективе это займет больше коллективных усилий по устранению проблем с конструкторами, чем если просто избавиться от проблемы прямо сейчас.

Понятно, сломанная совместимость всегда огорчит некоторых людей, даже если в рамках основных версий все останется нормальным, нарушать совместимость необходимо для целей прогресса. Представьте себе вой, который поднимется, когда подобные люди узнаю об этом. Блин, если бы в прошлом году не был бы обновлнен mysqli заместо mysql, то либо WordPress не смог бы корректно работать на PHP7, либо PHP7 был бы дырявым и безнадежно устаревшим по той единственной причине, что core-разработчики пожалели пользователей WP и решили оставить их счастливыми.

Мой совет тем, кто опасается PHP7 — стоп. Если вы не хотите обновляться, то и не делайте этого. Если вы сидели на 5.3 или 5.2 так долго (привет, любители CodeIgniter), то посидите на 5.6 еще десяток лет, не мешайте PHP быть современным. Оставьте прогресс для тех, кто готов принять его.

Что думаете вы по этому поводу? Вся эта затея с удалением артефактов бред или действительно нужный шаг?

В сторону: изменения Extension API

В качестве интересной заметки на полях, есть некоторые изменения в PHP7, которые могут стать причиной небольшой задержки с портированием расширений на версию 7. API для создания расширений попало под рефакторинг (чистку) и все может поменяться, тем не менее этот провакационный твит от Sara Golemon собрал немного внимания.

Damn. There are some serious surprises in the PHP7 Extension API changes. Not for nothin’, but it’s a good time to switch to HHVM.
— SaraMG (@SaraMG) January 3, 2015

Она в основном говорит о том, что изменения в создании расширений при переходе от 5.6 до 7 будет настолько большими, что проще узнать как делать расширения под HHVM. А затем она продолжает развивать тему серией статей на тему как создать HHVM extension.

Вы разрабатываете расширения? Изучали ли вы изменения и как вы относитесь к всему этому, не слишком ли рано сейчас говорить о будущем эффекте?

Edit: Мое внимание обратили на то, что Сара уже после всего этого начала документировать новое API расширений, совместимое и с HHVM и с PHP7. Похоже, можно делать расширения, совместимые с обоими рантаймами!

Вывод

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

Что вы думаете об этих RFC? Как вы относитесь PHP7 в целом? Движется ли он в правильном направлении? Дайте нам знать — мы хотим услышать ваши мысли!

Последнее обновление: 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";
?>

Понравилась статья? Поделить с друзьями:
  • Какие рекомендованы инструкцией способы проверки указателей напряжения перед их применением
  • Какие реквизиты необходимо оформить при составлении должностной инструкции
  • Какие реквизиты не входят в состав инструкции
  • Какие реквизиты входят в должностную инструкцию
  • Какие разделы содержит должностная инструкция