froot Posted September 19, 2020 Share Posted September 19, 2020 namespace ProcessWire; function myfunction() { echo $config->paths->assets; // Notice: Undefined variable: config (…) } can't access the API inside a custom function. Didn't have that problem before. This seems like a basic setting. What could be the reason? Link to comment Share on other sites More sharing options...
kongondo Posted September 19, 2020 Share Posted September 19, 2020 (edited) 2 hours ago, fruid said: Didn't have that problem before. Nope. It's never been possible. You must be confusing this with something else. Nothing to do with ProcessWire. Many (most?/all?) languages have variable scope. $config is outside the variable scope of myfunction(). myfunction() doesn't know what config is. You have to tell it. Having a namespace declaration has nothing to do with it. namespace ProcessWire; // alternative 1: use the global wire() function - @see: https://processwire.com/api/ref/functions/wire/ function myfunction() { echo wire('config')->paths->assets; // no error } // alternative 2: pass in $config function myfunction2($config) { echo $config->paths->assets; // no error } alternative 3: functions API - e.g. https://processwire.com/api/ref/functions/config/ @see also: https://processwire.com/api/ref/functions/ Edited September 19, 2020 by kongondo 5 Link to comment Share on other sites More sharing options...
froot Posted September 19, 2020 Author Share Posted September 19, 2020 it doesn't know $page or $pages either… Link to comment Share on other sites More sharing options...
BillH Posted September 20, 2020 Share Posted September 20, 2020 In what @kongondo suggests, replace "config" with "page" or "pages". Link to comment Share on other sites More sharing options...
Frank Vèssia Posted September 22, 2020 Share Posted September 22, 2020 On 9/19/2020 at 6:59 PM, fruid said: it doesn't know $page or $pages either… it doesn't work for the same reason $config doesn't work, use wire('page') inside functions Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 22, 2020 Share Posted September 22, 2020 In PHP functions do not inherit variables from the parent scope. Even closures (unlike many other languages) don't do so unless explicitly stated by `function() use ($var, …) {}` Link to comment Share on other sites More sharing options...
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