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)

AmateurP
  • 11
  • 1
  • how about using `flashdata` or `sessions` – tomexsans May 28 '13 at 14:48
  • put `$this->service = new ThirdLibray($varA,$varB);` in `__construct()` one point you need to take care of is, `$varA` and `$varB`. without looking at the context, it isn't easy to offer a solution. – itachi May 28 '13 at 14:59
  • itachi the problem with putting `$this->service = new ThirdLibray($varA,$varB);` in the `__construct()` is that each time when you hit a controller method in codeigniter (through AJAX) the constructor gets called and that eventually creates a new object of `$this->service = new ThirdLibray($varA,$varB);` each time. I tried that earlier. – AmateurP May 28 '13 at 15:07
  • @tomexsans storing the object in flashdata? that gives the same response after retrieving it in 2nd method (through 2nd AJAX call), i.e. `Call to a member function stopCall() on a non-object` – AmateurP May 28 '13 at 15:09

2 Answers2

0

store the object into session and use it.

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

 }
Rameshkrishnan S
  • 413
  • 1
  • 3
  • 8
  • That didn't help either, i had tried earlier but tried it once more after you suggested. So I still get the error `Call to a member function stopCall() on a non-object`. So, I just attempted to check if `$this->service` is getting same object,for this I did `var_dump($this->service)` once in ControllerA before `$this->session->set_userdata('lib_service', serialize($this->service));` and later in ControllerB after `$this->service = unserialize($this->session->userdata('lib_service'));`. In ControllerB i get `boolean false` as the response. :| – AmateurP May 28 '13 at 23:51
  • @AmateurP. pls try without using serialize and unserialize – Rameshkrishnan S May 29 '13 at 01:44
0

You can try with Singleton pattern for php. This is the basic code for implement it for your situation but I'm not sure it will work for you. Let's try:

  1. First create a wrraped class for your third party library: ThirdlibraryWrapped.php that you can see more about singleton 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. For your controller code:

    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();
     }}
    
Community
  • 1
  • 1
JamesN
  • 387
  • 1
  • 9