LoginSignup
0

More than 5 years have passed since last update.

こんなキャストが通るならphp7がやっぱいいなと思った

Posted at

これエラーにしてくれないんだということがあったので書いておこうかなと。。

こんなクラスがあって

class Hoge
{
      /**
     * @param int $id
     */
    public function setHogeId($id)
    {
        $this->hogelId = (int)$id;
    }

    /**
     * @retunr int $id
     */
    public function getHogeId()
    {
        return $this->hogelId;
    }
}

こう動かすとエラーも何も起きないんですね

$id = [1, 2];

$hoge = new Hoge();
$hoge->setHogeId($id);

var_dump($hoge->getHogeId()); // => int(1)

やっぱphp7が使えるものなら ↓のように書きたいですね。。

    /**
     * @param int $id
     */
    public function setHogeId(int $id)
    {
        $this->hogelId = $id;
    }

    /**
     * @retunr int $id
     */
    public function getHogeId(): int
    {
        return $this->hogelId;
    }

そもそもsetHogeId()に配列が入ってしまうような途中処理が悪いのだけど
それを検知できなくて他で思うように動かずハマりました。。

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
0