Are you running this outside of MODX? If so, you need to load and initialize modx before you can use it.
something like this:
<?php
// adjust this as necessary
$path = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/config/config.inc.php';
if (file_exists($path)) {
include($path);
} else {
die ('Could not find config.inc.php at ' . $path;
}
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = new modX();
/* Initialize and set up logging */
$modx->initialize('mgr'); // or $modx->initialize('web');
$modx->getService('error', 'error.modError', '', '');
$modx->setLogLevel(xPDO::LOG_LEVEL_INFO);
$modx->setLogTarget(XPDO_CLI_MODE
? 'ECHO'
: 'HTML');