3

I've created a module that connects to the database and retrieves some data using

$data = $db->loadRowList();

inside one of the member functions of the module helper class. Is there a way I can store that $data object as a member variable of the class so I can use it in other member functions of the class? My idea is to use $data in my Ajax functions. I've tried defining it like this :

class MyModuleHelper{
    private $data;

    public static MyDatabaseFunction($params)
    {
      ....
      self::$data = $db->loadRowList();
      ...
      //here self::$data is full
    }

    public static MyAjaxFunction()
    {
       //here self::$data is empty
    }
     .....
}
2

I think you're a little confused as to the relation between static PHP methods/variables and AJAX calls. When you make an AJAX call to the server it's not going to pull up a previously created version of your MyModuleHelper object (even if methods of that object are static). This is because the AJAX call is in a completely different application scope than when the page was loaded.

Think of it this way: two people load the page that has the module. Each page load resulted in a completely independent PHP application/thread being created/executed with a completely different instance of MyModuleHelper (regardless of whether it has static methods/variables). Each person then clicks the button that makes the AJAX call. How does the server know which AJAX call is tied to which MyModuleHelper instance? It doesn't. In fact, those first two PHP applications/executions don't even exist anymore (they completed their work and ended when the page finished loading). The server would create a new PHP application execution and for each AJAX call (with new instances of MyModuleHelper).

That's why your $data is always empty when you make the AJAX call. Static methods/variables are only useful when the same PHP application is using a class and you want to share data (via a static class variable) or use static methods.

The only way to do what you're intending is to make sure $data is populated in any of your function calls that you're calling via AJAX (no need to use static methods or variables):

class MyModuleHelper
{

  var $data = null;

  public populateData()
  {
     // no need to re-populate if we re-call this method for whatever reason
     if(is_object($this->data)) return;

     ...

     $this->data = $db->loadRowList();
  }


  public MyAjaxFunction()
  {
     $this->populateData();

     var_dump($this->data);

     // do stuff
  }


  public MyOtherAjaxFunction()
  {
     $this->populateData();

     var_dump($this->data);

     // do other stuff
  }

}

Hope this helps.

  • Thanks for the good answer! I'll have to think of a different approach to my problem because I think that getting data from database in each AJAX call is probably not a good idea. – VTodorov Oct 10 '17 at 16:34
  • I wouldn't worry too much about it. You'll need to grab the data from somewhere. Any other method would probably be slower or a bigger hassle. One option would be pre-loading the data after the page is loaded so that if the user clicks on one of the buttons it'll already be available client side. However, that means you'll be making a bunch of useless ajax calls if the user doesn't end up clicking those buttons. You could also preload the data with the original page load, but that'll increase the initial load time slightly. It's all about when you want to waste the time getting that data. – Drew G Oct 10 '17 at 16:43
  • 1
    BTW, I made like 5 edits to my original answer attempting to be more clear, so if anything didn't make sense give it a re-read, it might be a little easier to get. – Drew G Oct 10 '17 at 16:45
2

I don't know a lot about static methods as I never work with them, however (and don't quote me on this), it may be because you cannot access non-static properties in a static function.

So you'll either need to define you class property like this:

class MyModuleHelper
{
    static $data;
    ...
}

or move away from static methods, like so:

class MyModuleHelper
{
    private $data;

    public function MyDatabaseFunction($params)
    {
        $this->data = $db->loadRowList();
    }

    public function MyAjaxFunction()
    {
        var_dump($this->data);
    }
}

Hope this helps

  • Now I'm getting error: Using $this when not in object context in my Ajax function. – VTodorov Oct 10 '17 at 11:40
  • Could you share all the code on pastebin.com ? It's quite hard to tell without seeing everything – Lodder Oct 10 '17 at 11:50
  • There you go helper.php pastebin.com/gAfTxCmu , mod_display_vacancies.php pastebin.com/i0DRtD0Q and my javascript file pastebin.com/1vNuSyN3 . Thank you for your time! – VTodorov Oct 10 '17 at 12:07
  • Is the error coming from line 16? var_dump($this->vacanciesRows); – Lodder Oct 10 '17 at 12:21
  • Yeah. Everything else seems to work fine. I just want to be able to access the member variables inside methods called with ajax. – VTodorov Oct 10 '17 at 12:22
0

I have been writing a lot of ajax modules for work, so I can relate to what you are trying to accomplish with your static methods.

I think you may have a misunderstanding about how/when these static methods are being called.

Via the Joomla handling of ajax processes, the function that looks like:

public static function ModuleNameAjax() {

will be immediately called by default (this is a feature). It doesn't matter what order you write the static methods inside of class modModuleNameHelper { }. The ModuelNameAjax() function WILL BE called, and all other static or non-static functions will remain dormant/un-called until you explicitly call upon them.

If you wish to have immediate access to AND cache the data that is generated from the function MyDatabaseFunction($params), then you will need to call it from inside ModuleNameAjax().

class MyModuleHelper {
    private $data;

    public static MyDatabaseFunction($params) {
        ...
        self::$data = $db->loadRowList();
        ...
        //here self::$data is full
    }

    public static MyAjaxFunction() {
        //here self::$data is empty
        self::MyDatabaseFunction($params); // I'll assume you have access to $params
        //here self::$data should be full and can forever be called by any function
        // from within the class until php terminates the script.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.