Skip to main content
added 210 characters in body
Source Link
xenooooo
  • 1.2k
  • 2
  • 5
  • 10

The problem in run2 is that you are calling the method directly and you need to pass the an argument since test is expecting argument $a. To use the method as a callback function you need the pass it as an array which is the first value is $this and the second is the method name which is test :

privatepublic function test($a) { return ($a + 1); } public function run2() { $collection = collect([1,2,3]); $newCollection = $collection->map([$this,'test']); return $newCollection; } 

UPDATE

It only work if the method that you will call is public and it will not work if you use a private or protected method. If you use a private or protected if will throw a BadMethodCallException

The problem in run2 is that you are calling the method directly and you need to pass the an argument since test is expecting argument $a. To use the method as a callback function you need the pass it as an array which is the first value is $this and the second is the method name which is test :

private function test($a) { return ($a + 1); } public function run2() { $collection = collect([1,2,3]); $newCollection = $collection->map([$this,'test']); return $newCollection; } 

The problem in run2 is that you are calling the method directly and you need to pass the an argument since test is expecting argument $a. To use the method as a callback function you need the pass it as an array which is the first value is $this and the second is the method name which is test :

public function test($a) { return ($a + 1); } public function run2() { $collection = collect([1,2,3]); $newCollection = $collection->map([$this,'test']); return $newCollection; } 

UPDATE

It only work if the method that you will call is public and it will not work if you use a private or protected method. If you use a private or protected if will throw a BadMethodCallException

Source Link
xenooooo
  • 1.2k
  • 2
  • 5
  • 10

The problem in run2 is that you are calling the method directly and you need to pass the an argument since test is expecting argument $a. To use the method as a callback function you need the pass it as an array which is the first value is $this and the second is the method name which is test :

private function test($a) { return ($a + 1); } public function run2() { $collection = collect([1,2,3]); $newCollection = $collection->map([$this,'test']); return $newCollection; }