1

I am trying to call different methods within same controller written using Codeigniter through separate AJAX calls and create an object of a Third Library in one of those calls and later access the same object (also autoloaded SESSION library) in second method while making second AJAX Call:

Javascript has 2 separate AJAX Calls:

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

And the controller looks like:

     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();
     }

Now I know that PHP re-initializes the objects each time its loaded/visited, how could I make this work.How to ovecome such error:

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

(Querying here after making all the efforts searching and coding)

4

2 回答 2

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.
   }

 }
于 2013-05-28T18:09:17.270 回答
0

您可以尝试使用 php 的单例模式。这是针对您的情况实施它的基本代码,但我不确定它是否适合您。我们试试看:

  1. 首先为您的第三方库创建一个包装类:ThirdlibraryWrapped.php,您可以看到更多关于单例PHP 单例类的最佳实践

    类 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();
     }}
    
于 2013-05-29T02:54:54.013 回答