1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】関数名の末尾のコロン型(:array, : int, : stringなど)は何を表しているのか?

Posted at

Laravelで定義した関数名の末尾にコロン型(:array, : int, : stringなど)がついていたため、何の意味があるのか調べてみた。

結論からいうと、戻り値の型を指定している。

function 関数名(引数の型 引数):戻り値の型
    {
        処理
    }

:の後ろはスペースを開けてもいい。:intでも: intでもどちらでも問題ない。

指定した型と異なる場合はTypeErrorが発生する。

error
Errors with:
Fatal error: Uncaught TypeError: Return value of 関数名() must be an instance of 型

# 例1
function origin(string $path):string {
        処理
    }

# 例2
function counter(?string $src, int $number):int {
       処理
    }

# 例3
function page_arr(int $current, int $last):array {
       処理
    }

[PHP公式 戻り値](https://www.php.net/manual/ja/functions.returning-values.php#functions.returning-values.type-declaration) 下部の英語の部分に記載されている。
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?