0

I have a module that uses comAjax . Default uses the standard way of calling:

'option' : 'com_ajax',
'module' : 'available_stock',

This works fine and in the module calls the relevant ajax function returning data to default.

In my helper Ajax function I call a bunch of other functions to set up drop down boxes. It's organised like this

class modAvailableStockHelper{
public static function getAjax()
    {   
        function setUpDropDown1($id, $table, $inherit = true){
            }
        function setUpDropDown2($component, $view, $inherit = true){
            }
    }
}

When I set the page up initially I also need these drop down boxes. so in mod_available_stock.php I repeat these functions. Obviously this causes code duplication and means if i update one function I need to update the other one. This isn't a great way of doing things. I'd like to be able to create the functions once and then use the either at the start up:

in the mod_available_stock.php

$DropDown1 = modAvailableStockHelper::setUpDropDown1($id, $table, $inherit);

and then in the getAjax() also call the same function.

Is this possible. I can only think of doing it by having a separate file with the functions in it and then including the file.

thanks

1

I think what you're looking for is self which can allow your static methods to call other static methods. Without any indication of state and db calls in your question it's difficult to tell what the various methods need.

It is uncommon to use global functions at all in Joomla, and 10 times as uncommon to declare functions in a method. Do let me know if the following restructuring is inadequate.

class modAvailableStockHelper{

    public static function getAjax()
    {   
        // some code

        $x = self::setUpDropDown1($id, $table, $inherit);

        $y = self::setUpDropDown2($id, $table, $inherit);


        // more code
        return $something;

    }

    public static function setUpDropDown1($id, $table, $inherit = true)
    {
        // code
        return $something;
    }

    public static function setUpDropDown2($component, $view, $inherit = true)
    {
        // code
        return $something;        
    }
}

From outside the class you can still access the methods statically as long as the helper file has been included.

$dropdown1 = modAvailableStockHelper::setUpDropDown1($id, $table, $inherit);
  • Thanks this worked great. I needed the function for drop down boxes. The initial php file creates boxes that are then updated during an ajax call. i didn't want to have the code repeated. – user1616338 2 days ago

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.