Pretty sure this is an issue of scope. I have a snippet with various functions. By both using the global keyword or by passing in the $modx object as a parameter to my function I can access all its methods with no issue.
Where I'm having a problem is accessing static constants such as modX::LOG_LEVEL_ERROR so I can write to the error log in the course of executing a function. If I call $modx->log(modX::LOG_LEVE_ERROR,'test') inside a function I get no log entry. However, if I call $modx->log('1,'test'); I get my expected output in the log so it does seem to me that my function can't access the constant - how to get around this is the question. Ideas?
[ed. note: freelancewebdev last edited this post 14 years, 5 months ago.]
Thanks but no, was just a typo there. The problem is that when you refer to modX::LOG_LEVEL_X you are calling a static constant member of the modX class - the modX class isn't initialised - due to scope, PHP can't find the class.
Fortunately, the solution is simple, you can also call static constants on a instantiated class so in this case you just use $modx->LOG_LEVEL_X rather than the static equivalent.
-
MODX Staff
- 10,725 Posts
Once the modX class is included, all the constants are available to you. This is not a scope issue unless you have not included the modX class yet.
Your tweet said is was in a snippet. If that's the case $modx and its contants are always available in one of these ways:
myFunction() {
global $modx;
$modx->whatever();
}
myFunction($modx);
myFunction( &$modx) {
$modx->whatever();
}
------------------------------------------------------------------------------------------
PLEASE, PLEASE specify the version of MODX you are using.
MODX info for everyone:
http://bobsguides.com/modx.html
[ed. note: BobRay last edited this post 14 years, 5 months ago.]
OK, you apparently have the $modx object, but not the constants (which seems impossible since they are part of the xPDO class and the modX class extends it).
It shouldn't make any difference, but try this:
$modx->log(xPDO::LOG_LEVEL_ERROR,'some string');
Inside the function xPDO::LOG_LEVEL_INFO evaluates to 3 so some progress but still no output in log file when I pass xPDO::LOG_LEVEL_INFO to the $modx->log() function - hmmmmmmmz
Right, both xPDO::LOG_LEVEL_ERROR and modX::LOG_LEVEL_ERROR are both available in my function, but when I pass either to the $modx->log() function I get nothing in the error log...
Ahhhh, OK, I see what you were getting at now Bob on Twitter - so if I understand you right, I can only put entries of modX::LOG_LEVEL_ERROR in the error log, not modX::LOG_LEVEL_INFO.