0

I'm having an issue using the parameters of my module to do anything specific. I keep getting the following error from my code:

PHP Fatal error:  Call to a member function get() on a non-object

This error occurs in the following file, on line 10:

<?php
echo($params)
class modSystemValidation {

function buildForm() {
    $form = '';
    $tabIndex = 0;

     if($params->get('includeGender','0') == 1 ) {
        $form .= '<div id="gender"><input type="radio" name="gender" id="gender_m" value="m" tabindex="' . $tabIndex . '" class="label" /><label for="gender_m">Male</label><input type="radio" name="gender" id="gender_f" value="f" tabindex="' . $tabIndex + 1 . '" /><label for="gender_f"></label></div>'; 

        $tabIndex++;
        $tabIndex++;
    }
  }
return $form
}

modSystemValidation::buildForm();

?>

I've been able to conclude that this is what params echos as to the page with an echo($params):

{"includeFirstName":"1","includeLastName":"1","includeEmail":"1","includePhone":"0","includeAddress1":"0","includeAddress2":"0","includeZip":"0","includeCity":"0","includeState":"0","includeGender":"0","validateFirstName":"0","validateLastName":"0","validateEmail":"0","validatePhone":"0","validateAddress1":"0","validateAddress2":"0","validateZip":"0","validateCity":"0","validateState":"0","validateGender":"0"}

I think my issue is that the $params variable is an array and not an object, but I seem to be having difficulties confirming this with the is_array() and is_object() functions.

A quick overview here before anyone answers this question:

  1. I know $params is defined
  2. My issue here is that I need a way to look at $params and do something based off its content.
  3. While I appreciate the help in structuring my code correctly for Joomla standards, i'd like to hold off on that until I have this issue resolved.

Any help would be greatly appreciated, I'm simply trying to access the params of my module and build form values based off of whether or not the user has enabled that specific field. Right now I know that with the present code the gender field would not be placed on the page, and that is expected. What i'm trying to solve is why I am getting the error on line 10 with the get function. $params is able to be echoed outside the class, but not inside it? Which does not make sense to me, and seems to be where I am getting my issue.

Xenology
  • 2,357
  • 2
  • 21
  • 39
  • var_dump($params) object(JRegistry)#585 (1) { ["data":protected]=> object(stdClass)#584 (8) { ["moduleclass_sfx"]=> string(0) "" ["showHome"]=> string(1) "1" ["homeText"]=> string(4) "Home" ["showComponent"]=> string(1) "1" ["separator"]=> string(0) "" ["cache"]=> string(1) "1" ["cache_time"]=> string(3) "900" ["cachemode"]=> string(6) "itemid" } } JRegistry (object), protected so you need to get them. Please don't use globals. – Elin Feb 01 '13 at 03:07
  • I'm not entirely sure what you're saying here, I updated my question to not include the global variables. Still to no avail with the help offered in this question so far i'm still getting the PHP Fatal Error – Xenology Feb 01 '13 at 18:13
  • $params is clearly a JRegistry object as you can see from the var_dump. THe properties are protected you need to use the getter to access them. – Elin Feb 02 '13 at 14:34

2 Answers2

1

In your var_dump() of $params there is no includeGender - only a validateGender. If the parameter name of includeGender doesn't exist - then you are going to get a php fatal error from the statement.

Try something like this on line 9 (note if you genuinely meant includeGender then you need to check your parameter names!

if($params->get('validateGender','0') == 1 ) {

Also @Lodder is technically right this should be in a helper.php file with a helper class to adhere to Joomla standards - not as a function in the tpml.php file. However this won't make a difference to the coding error - it just makes your module adhere to Joomla standards a bit better!


UPDATE

Lodder got it slightly wrong - failed to notice it last night.

In the helper.php file you need to place the function:

class modSystemValidationHelper {

    function my_function($params) { 
         $form = '';
         $tabIndex = 0;

         if($params->get('includeGender','0') == 1 ) {
              $form .= '<div id="gender"><input type="radio" name="gender" id="gender_m" value="m" tabindex="' . $tabIndex . '" class="label" /><label for="gender_m">Male</label><input type="radio" name="gender" id="gender_f" value="f" tabindex="' . $tabIndex + 1 . '" /><label for="gender_f"></label></div>'; 

              $tabIndex++;
              $tabIndex++;
         }

    }

}

Then in your default.php file of the view you then call the function e.g.

 modSystemValidationHelper::buildForm($params);
George Wilson
  • 5,595
  • 5
  • 29
  • 42
  • I went back and edited my settings to include the includeGender variable, and it did not solve the issue. – Xenology Feb 01 '13 at 18:09
  • @Xenology Is it still showing the same error now the param exists? – George Wilson Feb 01 '13 at 18:21
  • Yes, if you look at my updated question you can see that I've changed it to reflect the new way the plugin is structured taking the advice from everyone thats offered help on this question. I think my issue now is that params is defined as a global variable, because i can echo it outside the class, and get the proper information, but within the class, and the function is where I get the PHP Fatal Error. That leads me to believe there must be something I have to do to be able to access that variable within the class / function. – Xenology Feb 01 '13 at 18:23
  • Dear christ >.< was I really that blind? – Xenology Feb 01 '13 at 18:40
  • So I seem to be getting past that error now, but am running into another error completely unrelated to this, thank you so much for your help! I don't suppose that you'd be available for a quick consult on the issue i'm still seeing? – Xenology Feb 01 '13 at 18:48
  • Scratch that looks like I got it all figured out! Thanks again! – Xenology Feb 01 '13 at 19:06
  • No worries! Glad to have helped! – George Wilson Feb 01 '13 at 19:14
0

You should put all your functions in a helper.php file like so:

class modSystemValidation {

    function my_function() { 

    }

}

You can simply the function like so in the default.php

modSystemValidation::my_function();

No need to use: global $form , $tabIndex, $params;

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Lodder
  • 19,758
  • 10
  • 59
  • 100
  • Well typically you have a method that takes $params as an argument. – Elin Feb 01 '13 at 03:02
  • Edited my questions to represent the changes to my plugin, its now within a helper.php file and within its own class. Still this does not seem to have solved my issue, although it seems to have gotten me closer. I now know that I have access to $params outside of the class but not within it, which makes me wonder what exactly is happened to that value. – Xenology Feb 01 '13 at 18:11