Reputation:
After few hours of googling and searching for answer, i gave up and got very frustrated with php, no support out there on basic errors. Anyway, here is my function, what am I doing wrong?
$accessToken = function(){
$array = array(
"foo" => "bar",
"bar" => "foo"
);
$obj = json_encode($array);
$obj = json_decode($obj);
return $obj->foo;
};
$getInfo = function(){
$code = $accessToken();
return $code;
};
$getInfo();
I get error
Notice: Undefined variable: accessToken in C:\inetpub\wwwroot\mysite\lab\cfhttp.php on line 43
Fatal error: Function name must be a string in C:\inetpub\wwwroot\mysite\lab\cfhttp.php on line 43
Upvotes: 0
Views: 121
Reputation: 7661
In PHP you usually define a function in another fashion. Try the following:
function accessToken() {
$array = array(
"foo" => "bar",
"bar" => "foo"
);
$obj = json_encode($array);
$obj = json_decode($obj);
return $obj->foo;
}
function getInfo() {
$code = accessToken();
return $code;
}
getInfo();
You can also take a look here http://php.net/manual/en/functions.variable-functions.php and http://php.net/manual/en/functions.user-defined.php
Upvotes: 0
Reputation: 212412
$accessToken
isnt in scope inside $getInfo()
$accessToken = function(){
$array = array(
"foo" => "bar",
"bar" => "foo"
);
$obj = json_encode($array);
$obj = json_decode($obj);
return $obj->foo;
};
$getInfo = function($accessTokenFunction){
$code = $accessTokenFunction();
return $code;
};
$getInfo($accessToken);
Upvotes: 3
Reputation: 4442
use ()
to take a variable inside this scope
$getInfo = function() use ($accessToken) {
$code = $accessToken();
return $code;
};
Upvotes: 1