This question already has answers here:
Anonymous recursive PHP functions
(6 answers)
Closed 7 years ago.
$function = function($parameter) use ($variable)
{
//Do some stuff
$function($something); //When I do this i get variable undefined '$function'
}
How can I call the function?
I've already tried to call the parent or redefine the variable but i'd like a more compact solution than just redefining a whole new function and using that.
The general trick here is to use () the Closure holding variable by reference:
$function = function($parameter) use ($variable, &$function) {
//Do some stuff
$function($something);
}
Related
This question already has answers here:
PHP Anonymous functions: undefined variable
(2 answers)
Closed 6 days ago.
I am trying to define a global variable to use in multiple Laravel routes. However, I am getting "undefined variable $title" as error message. What am I doing wrong? The code on the bottom, throws a similar error.
$title = "YAAA";
Route::get('/insertORM', function(){
$a = $title;
});
This piece of code exists in a standard laravel application inside the routes/web.php file.
The function needs to inherit the variable from the parent scope by using use()
Route::get('/insertORM', function() use ($title, $body) {
$a = $title;
$b = $body;
});
This question already has answers here:
Static variables in PHP
(5 answers)
Closed 11 months ago.
I found the following method and I don't know what the static keyword does at that point. The variable is initialized with [] so it's empty anytime the method is called anyways.
What does the static keyword do at this point?
public function getSomething($entity)
{
static $collection = [];
if (!$collection[$entity->getPrimaryKey()]) {
$collection[$entity->getPrimaryKey()] = 'something';
}
return $this->doCollection($collection);
}
declaring properties or methods as static allows you to access them without needing to instantiate the class.
This question already has answers here:
Dynamic static method call in PHP?
(9 answers)
Closed 6 years ago.
I have a couple of methods whose returns are being cached, and the cache key is the name of the method itself.
For instance, if this is my class
class tester {
static function test() {
$data = build_data();
cache(__METHOD__, $data);
}
}
The cache key value is tester::test.
I am implementing functionality to warm the cache. If I have all the cache keys, I could just call them one by one.
foreach ( $keys as $key ) {
$key();
}
But apparently, I can't call a string like 'tester::test' in this manner
Fatal error: Call to undefined function tester::test() ...
Do I have to do string parsing, to pull apart the class name and method, and then call them like $class::$method()? Or is there a simpler way to do it?
Thanks to Michael Lihs for linking the question in their comment; it turns out that call_user_func() does what I'm looking for.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
Why does echoing the variable from test1() in test2() in the following code not work? I thought if I execute a function in another one it is jsut as if the code was placed in there.
And how can I do it to make it work?
function test1() {
$var = "Hallo";
}
function test2() {
var();
global $var;
echo $var;
}
test2() ;
The $var variable isn't in the scope of the test2() function.
See: this post for details.
This question already has answers here:
Using `$this` in an anonymous function in PHP pre 5.4.0
(5 answers)
Closed 9 years ago.
if want to do this following:
$filteredValues = array_filter($rawValues, function($rawValue) {
return $this->validateValue($rawValue);
});
validateValue is a private method in the same class.
How can i use $this context in array_filter in this way?
If you use PHP 5.3, PHP does not recognize $this as closure, you need to do a trick like JavaScript:
$self = $this;
$filteredValues = array_filter($rawValues, function($rawValue) use ($self) {
return $self->validateValue($rawValue);
});
Note that the above will only give you access to the object through the public API of the object. This is different from the 5.4 support for Closure rebinding which allows full access to $this.