douwan4993 2010-02-22 21:11
浏览 37
已采纳

为什么课程会多次重新申报?

Ok here is a method I use for initializing models in my controller actions:

protected $_tables = array();

protected function _getTable($table)
{
    if (false === array_key_exists($table, $this->_tables)) {
        include APPLICATION_PATH . '/modules/'
        . $this->_request->getModuleName() . '/models/' . $table . '.php';
        $this->_tables[$table] = new $table();
        echo 'test ';
    }
    return $this->_tables[$table];
}

Then when I call the _getTable() method two times (for example once in init() method and once in the controller action) it prints:

test test test test test test

On top of the page. Shouldn't it just return the object from the _tables array() because of the array_key_exists() check? In other words shouldn't the part inside the array_key_exists() function get executed only once when the method is called multiple times?

UPDATE:

So the problem is this - for some reason the layout gets printed twice (so it's layout printed and inside the layout where there is layout()->content; ?> it prints the layout again). I have no idea why it does this as it worked well on the previous server and also on localhost.

  • 写回答

2条回答 默认 最新

  • douyu4822 2010-02-22 21:51
    关注

    In the snippet you show:

    protected $this->_tables = array();
    

    This is not valid syntax, it should be:

    protected $_tables = array();
    

    Also, why not just use include_once and let PHP handle this for you? Alternatively, you could use the Zend_Loader. Don't reinvent the wheel.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题