Robin S Posted November 24, 2016 Posted November 24, 2016 Is it possible to define a variable in the place that the hook is attached (e.g. ready.php or a template file) and then access that variable inside the hook function? $animal = 'cat'; $this->addHookAfter('Class::method', function($event) { // is there some way to use $animal here? }); I tried passing the variable as a second argument in the anonymous function but got an error. Missing argument 2 for ProcessWire\ProcessWire::{closure}()
adrian Posted November 24, 2016 Posted November 24, 2016 You can use $this->animal $this->animal = 'cat'; $this->addHookAfter('Page::render', function($event) { bd($this->animal); }); or you can also do this: $animal = 'cat'; $this->addHookAfter('Page::render', function($event) use($animal) { bd($animal); }); 7
kongondo Posted November 24, 2016 Posted November 24, 2016 $animal = 'cat'; $this->addHookAfter('Class::method', function($event) use($animal) { // is there some way to use $animal here? Yes....use 'use' echo $animal; }); Edit...OK, so I am too slow.... 3
Robin S Posted November 24, 2016 Author Posted November 24, 2016 Thanks @adrian, I knew there would be a simple solution. And, thanks @kongondo 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now