You could try putting ’global $modx’ at the top of your function, so it can see the $modx object, however in my experience this doesn’t always work, I’ve called external php code from snippets where this doesn’t seem to work, try it anyway, it may be OK in a plugin.
Like this :-
function myFunction()
{
global $modx;
$result = $modx->db->select("*", "modx_site_content","");
}
Use MODx, or the cat gets it!
Right. The scope of the $modx variable doesn’t reach inside functions so without global $modx, PHP creates a new variable called $modx that’s empty and not very useful. global $modx tells PHP that you mean the variable created outside the function.
I’ve also encountered situations where it doesn’t work (nested includes/functions I think), but have never had the time to track down why and usually just pass the $modx variable down as an argument.
Yes, passing $modx as a variable is what I ended up doing, but like you say I never really sussed why sometimes you need to do this.
Use MODx, or the cat gets it!