通过Codeigniter中的ajax调用不同方法的对象引用

时间:2013-05-28 14:41:32

标签: php ajax codeigniter session-variables

我试图在使用Codeigniter通过单独的AJAX调用编写的同一个控制器中调用不同的方法,并在其中一个调用中创建第三个库的对象,然后在第二个方法中访问同一个对象(也是自动加载的SESSION库)第二个AJAX呼叫:

Javascript有两个独立的AJAX调用:

     $.post('http://mysite.com/UserController/controllerA',{Param1:x,Param2:y});
     $.post('http://mysite.com/UserController/controllerB');

控制器看起来像:

     require_once 'FILEPATH/Thirdlibrary.php';        
     class UserController extends CI_Controller {

     protected $service;

     public controllerA{
       $varA = $this->input->post('Param1');
       $varB = $this->input->post('Param2');

       $this->service = new ThirdLibray($varA,$varB);

       $this->service->startCall();//Thirdlibrary has method startCall() and stopCall
     }

     public controllerB{
       //I want to refer to the same object $this->service here

       $this->service->stopCall();
     }

现在我知道PHP每次加载/访问时都会重新初始化对象,我怎样才能使这个工作。如何产生这样的错误:

    Call to a member function stopCall() on a non-object

(在完成所有搜索和编码工作后查询此处)

2 个答案:

答案 0 :(得分:0)

将对象存储到会话中并使用它。

 require_once 'FILEPATH/Thirdlibrary.php';        
 class UserController extends CI_Controller {

 protected $service;

 public controllerA{
   $varA = $this->input->post('Param1');
   $varB = $this->input->post('Param2');

   if($this->session->userdata('lib_service'))
   {
       $this->service =$this->session->userdata('lib_service');
   }
   else
   {
       $this->service = new ThirdLibray($varA,$varB);
   }
   $this->service->startCall();//Thirdlibrary has method startCall() and stopCall
   $this->session->set_userdata('lib_service',$this->service);   // Why i am saving the object here?, startCall may update some object properties, so  that-also will be saved. Otherwise you can have this line in the else part above, after object creation.
 }

 public controllerB{
   //I want to refer to the same object $this->service here
   if($this->session->userdata('lib_service'))
   {
       $this->service =$this->session->userdata('lib_service');
       $this->service->stopCall();
       // Here after stop if you don't want the object, you can clear from session.
   }

 }

答案 1 :(得分:0)

您可以尝试使用Singleton模式进行php。这是为您的情况实现它的基本代码,但我不确定它是否适合您。我们试试吧:

  1. 首先为您的第三方库创建一个wrraped类:ThirdlibraryWrapped.php,您可以看到有关单身Best practice on PHP singleton classes的更多信息

    class ThirdLibraryWrapped {

    public ThirdLibray thirdLibrary;
    
    public static function Instance($param1=null,$param2 = null)
    {
        static $inst = null;
        if ($inst === null) {
            $inst = new ThirdlibraryWrapped($param1,$param2);
        }
        return $inst;
    }
    
    private function __construct($param1,$param2)
    {
         if($param1!=null && $param2 != null)
              thirdLibrary = new ThirdLibrary($param1,$param2);
         else
              //default constructor of thirdLibrary
              thirdLibrary = new ThirdLibrary();
    }}
    
  2. 对于您的控制器代码:

    require_once 'FILEPATH/ThirdlibraryWrapped.php';        
    class UserController extends CI_Controller {
     protected $service;
     public controllerA{
       $varA = $this->input->post('Param1');
       $varB = $this->input->post('Param2');
    
       $this->service = ThirdlibraryWrapped::Instance($varA,$varB);
    
       $this->service->thirdLibrary->startCall();//Thirdlibrary has method startCall() and stopCall
     }
    
     public controllerB{
       //I want to refer to the same object $this->service here
        $this->service = ThirdlibraryWrapped::Instance();
        $this->service->thirdLibrary->stopCall();
     }}