【发布时间】:2014-04-09 11:19:37
【问题描述】:
这篇文章可能很长而且很乱,但是哦……所以我有我的原始 PHP 网站
我原来的文件结构:
/Index.php
/Users.php
/Smarty.class.php
/db.php
/Stats.php
/css/
/js/
现在我想将每个索引文件“移植”到 phalcon 控制器中,如下所示:
/Controllers/IndexController.php
/Controllers/UsersController.php
/Smarty.class.php
/db.php
/css/
/js/
问题是全局关键字在 IndexController.php 中不起作用:
class IndexController extends \Phalcon\Mvc\Controller
{
include "/db.php"; // $db is initialized there
include_once ("Smarty.class.php");
$main_smarty = new Smarty;
public function indexAction()
{
function doSearch($limit) {
global $db, $current_user, $main_smarty; // db and smarty objects
$db->get_results("// my query"");
$search_clause = $this->get_search_clause();
$main_smarty->assign('search', $this->searchTerm);
}
}
}
致命错误:在非对象上调用成员函数 get_results()
但它在我的原始代码中运行良好,没有 Phalcon。
include "/db.php";
function doSearch($limit) {
global $db, $current_user, $main_smarty;
$search_clause = $this->get_search_clause();
$main_smarty->assign('search', $this->searchTerm);
我的引导程序(Index.php)文件:
try {
//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
'Controllers/',
))->register();
// DI
$di = new Phalcon\DI\FactoryDefault();
// View component
$di->set('view', function(){
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('/');
return $view;
});
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo "PhalconException: ", $e->getMessage();
}
【问题讨论】:
标签: php global-variables phalcon