Reputation: 2452
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:
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.
Upvotes: 0
Views: 1471
Reputation: 19733
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;
Upvotes: 0
Reputation: 5705
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);
Upvotes: 1