0
0

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 1 year has passed since last update.

Doctrine forAllでmust be of type ○○, int given,が出る

Posted at

サンプルコード(エラーがでる)

$collection=new ArrayCollection(["a","b","c"]);
if($collection->forAll(function(Hoge $hoge){
   //何か処理
}

エラー内容

Argument #1 ($hoge) must be of type Hoge, int given

背景

filterやmapなどと違い、forAllは与えられた無名関数に(key,value)の組を与えます。
ドキュメント:https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html#forall

#修正

$collection=new ArrayCollection(["a","b","c"]);
if($collection->forAll(function(int $key,Hoge $hoge){
   //何か処理
}

#あとがき
filterとかから書き換えたときに陥りがちなエラーで、ドキュメントを見れば一発ですが意外と気づかないもんですね。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?