LoginSignup
1
0

More than 5 years have passed since last update.

PHP7におけるタイプヒンティングにて発生した例外をcatchする

Last updated at Posted at 2018-08-29

呼び出し元

    try{
        $users = $this->users->fetch_users($id);
    }catch (TypeError $e){
        echo $e->getMessage();
    }

呼び出し先

    public function fetch_users(int $id)
    {
            $sql = "SELECT * FROM users WHERE id = ?"; //
            $users= $this->db->query($sql, $id);
            return $users;
    }

結果
Argument 1 passed to XXX::fetch_users() must be of the type integer, string given, called in XXX/xxx/xxx/xxx.php on line

なお本例のようにint型を指定しても、「1abcd」など数値始まりの文字列が渡された場合、異なったエラーが発生し補足できない模様

1
0
2

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
0