I use the map function to loop each object of collections. The closure method is too long and I tried to make it a private function. But I cannot successfully call the function.
Here is my sample code. (note: the function test may be very long in real case)
<?php
class cls
{
private function test($a) {
return ($a + 1);
}
public function run1() {
return ($this->test(5));
}
public function run2() {
$col = Collect([1,2,3]);
return ($col->map($this->test()));
}
public function run3() {
$col = Collect([1,2,3]);
$mycls = $this;
return ($col->map(function ($c) use ($mycls) {
return($mycls->test($c));
}));
}
}
$c = new cls;
$c->run1(); # 6
$c->run2(); # Error: Too Few Argements
$c->run3(); # [2,3,4]
I use the function: run1 to test if the private function is callable and I failed at function: run2. Although function: run3 makes code shorten. It seems a little bit too superfluous. How can I make run2 works?
Update
My version of Laravel is 6.2
Update
I tried the @xenooooo answer.
It works with the public function, but I get a different error code with the private method.