I trying to understand closures more but I cannot get the correct response from the below code. I get a response of 31 instead of 60 for some reason. My aim is to eventually start unit testing closures.
Thanks
<?php
class Closuretest
{
/**
* Closuretest constructor.
*/
public function __construct()
{
}
/**
* @return mixed
*/
public function getToken()
{
$response = $this->getEntry('abcde', function() {
return 30;
}, 30);
// Return access token
return $response;
}
/**
* @param $a
* @param $b
* @param $c
* @return mixed
*/
private function getEntry($a, $b, $c)
{
return $b+$c;
}
}
$testinstance = new Closuretest();
echo $testinstance->getToken();
getEntrymethod, you are getting the sum of $b and $c. You passed a closure on the second argument so it becomes closure + 30. If you allow notices in your error verbosity you will get aPHP Notice: Object of class Closure could not be converted to intnotice, and interprets the closure as 1, that's why you're getting 31.