The title may be a bit confusing, so let me explain. I have a class and a method. That that method itself has a function. Like this:
class MyClass {
public static function my_function($param) {
function nested_function() {
//do something with $param
}
}
}
So my question is, how can I access $param in nested_function?
EDIT
The reason I’m doing this is because of a wordpress thing. I need something like this:
class MyClass {
public static function my_function($param) {
function nested_function() {
//do something with $param
}
add_action('init', 'nested_function');
}
}
This is not a sensible thing to do. All named functions in PHP are global. Nesting them inside something else just makes their declaration conditional. I.e. you cannot call
nested_functionuntil you have calledMyClass::my_function. And afterwards you cannot callMyClass::my_functionagain, because PHP will try to redeclare an existing function.Maybe you want a closure, i.e. an anonymous function, which can include variables from the parent scope: