modx function

Добрый вечер. Уже который час пытаюсь понять где я допускаю ошибку.
function getlastid() {
    $q=$modx->prepare("SELECT `id` from `modx_site_content` order by `id` DESC limit 1");
	$q->execute();
	$lastid = $q->fetchAll(PDO::FETCH_ASSOC);
	return $lastid[0]['id'];
}

echo getlastid();
Выдает
Fatal error: Call to a member function prepare() on a non-object in /var/www/firstopt/data/www/firstopt.ru/core/cache/includes/elements/modsnippet/53.include.cache.php on line 11
Если же убрать function и сделать
$q=$modx->prepare("SELECT `id` from `modx_site_content` order by `id` DESC limit 1");
	$q->execute();
	$lastid = $q->fetchAll(PDO::FETCH_ASSOC);
print_r($lastid);
нет проблем, все отлично… Что я делаю не так?
Николай
03 марта 2014, 15:48
modx.pro
1 772
0

Комментарии: 1

Василий Наумкин
03 марта 2014, 19:58
+1
Проблема с областью видимости переменной $modx, которой нет в функции.

Нужно или так:
function getlastid($modx) {
	//...
}
getlastid($modx);
Или
function getlastid() {
	global $modx;
	//...
}
    Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
    1