functions in a calc element

softforge

Member
I have made a calc element with several ajax watch elements and because there are several common areas of code I made some into functions in the calc element nd passed the watch elements to the function.
All works beautifully doing the calculation and displaying the result...until I save, at which point I get a 500 server error. So I stripped it back and just added a simple function thats not even called

function foo()
{
echo "I don't exist until program execution reaches me.\n";
}

and just that causes the 500 on save

Is there any way to include functions in calc elements or do we have to go through each each element in its on block of code
 
The problem is that your calc code can be called multiple times. And functions can only be defined one time. So the second time the code runs, you'll get a fatal "cannot redeclare function" error.

Two ways round it. What I do, which is probably the "best" way, is create a custom class somewhere, like ./components/com_fabrik/myclass.php ...

Code:
class MyClass
{
   public static function doMyThing($formModel, $foo)
   {
      // do my thing with $formModel and $foo
   }
}

... then wherever I need to use functions from it ...

Code:
require_once JPATH_BASE . '/components/com_fabrik/myclass.php';
MyClass::doMyThing($formModel, 'whatever');

The "require_once" then takes care of any issues with redeclaring stuff. And you can then collect all your custom code in one place, and anywhere you need to use custom code (eval'ed element defaults, calcs, email plugins, etc), use a function in your class.

Or, simpler but less useful in the long run, wrap a function_exists() around your function decleration ...

Code:
if (!function_exists('foo')) {
   function foo() {
      // do foo
   }
}

foo();

-- hugh
 
Thank you Hugh for a very well detailed answer which is both elegant and ....works
That has indeed fixed the problem.
I used my function again in another calc earlier and realised that the 500 I was getting was because of the double function decloration. I had not realised that just having it once int he one calc element would cause the same issue on save.

Can I just say that with years of going in various forums, the answers in these forums from both you and Rob (who I met at JandBeyond a few years ago) are some of the best, most detailed and patient responses.
Thank you
 
Thank you Hugh for a very well detailed answer which is both elegant and ....works
That has indeed fixed the problem.
I used my function again in another calc earlier and realised that the 500 I was getting was because of the double function decloration. I had not realised that just having it once int he one calc element would cause the same issue on save.

Can I just say that with years of going in various forums, the answers in these forums from both you and Rob (who I met at JandBeyond a few years ago) are some of the best, most detailed and patient responses.
Thank you
Thank you. And thanks for being a subscriber.

Hugh

Sent from my HTC6545LVW using Tapatalk
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top