如何在 AJAX 调用之间保持 PHP 对象存活(或在内存中)

How to keep PHP object alive (or in memory) between AJAX calls

我有以下 class 定义:

class DatasegmentationController
{
    public function indexAction()
    {
        $options['permissions'] = array(
            'canDelete'     => false,
            'canEdit'       => false
        );

        if ($this->getRequest()->isXmlHttpRequest()) {
            $table = $this->getRequest()->getParam('table');

            if ($table !== '' && $table !== null) {
                $utilStr = new UtilString();

                // This is a patch because class and tables names does not match
                // so far it only happens with company and this is only for
                // instantiate the proper class dynamically
                $param_table = $table;
                $table       = $table === 'companies' ? 'company' : $table;
                $classObj    = strpos($table, '_') !== false ? $utilStr->stringToCamelCase($table, '_') : $utilStr->stringToCamelCase($table);
                $className   = new $classObj();

                $module_map = $field_map[$param_table];

                /** @var  $module_map array */
                $fields = [];
                foreach ($module_map as $key => $value) {
                    $fields[] = [
                        'id'   => $key,
                        'text' => $key
                    ];
                }

                $conditions      = json_decode($this->_request->getParam('conditions'), true);
                $dynDataGridName = "DataSegmentation{$this->classObj}Grid";
                $dynMethodName   = "get{$this->classObj}GridModel";

                $gridObj = new $dynDataGridName(
                    $this->className->$dynMethodName($conditions),
                    $this->view->users_id,
                    "{$table}_list",
                    "{$table}.{$table}_id",
                    '/datasegmentation/index',
                    'editor',
                    $options
                );

                return $this->_helper->json([
                    'fields' => $fields,
                    'grid'   => $gridObj->getGridJs()
                ]);
            }

            if (isset($classObj, $className, $gridObj)) {
                $page  = $this->_request->getParam('page', 1);
                $limit = $this->_request->getParam('rows', 20);
                $col   = $this->_request->getParam('sidx', 1);
                $order = $this->_request->getParam('sord', 0);
                $search  = $this->_request->getParam('val', null);

                echo $gridObj->getData($page, $limit, $col, $order, $search);
            }
        }
    }
}

以上代码的作用如下:

遇到这种情况我的问题是:

问题

我不知道这是否有用,但正在使用 PHP 5.5.x.

在 Zend Framework 1 项目之上进行测试和开发

How do I keep the object alives between AJAX calls? Storing in a session var? Any other workaround?

将数据存储在会话变量中 使用客户端 ID(可以是登录名、随机数、IP 等)将数据存储在文件中 将数据存储在数据库中。

If the answer is store them in a session var, is it recommendable? What about the answers on this, this and this among others?

如果您要存储关键数据,请使用端到端加密、SSL、HTTPS。

How would you handle this?

使用会话变量。