[SOLVED] Can't call methods from a constructor
Posted: Wed Apr 14, 2010 7:17 pm
Hello! I have a class that contains one method that returns a value based on the parameters submitted to the function. I want to create a constructor that takes the necessary values needed for the function, and then runs the function. So I tried creating a constructor that takes the parameters necessary for the function, and then runs the function from the constructor so that I only need to instantiate the class with the proper variables passed, and it will return the proper values. For some reason, I can't seem to successfully run the function from the constructor! I've tried running the function using just functionName(); and I've also tried using $this->functionName(); and self::functionName(); but no matter what I do, it doesn't seem to call the function. When I create a new instance of a class in a variable and then call the function from that variable, everything works. Here's an example of the code:
I want to call the function immediately after the instantiation of the class, but that doesn't seem to work. See the following code:
The variable dump show that $status is a ParamsChecker object, but the value returned by the CheckParams() function isn't returned. If I remove the constructor and call the method the following way everything works:
I'm not sure what I'm doing wrong, but I'd like to be able to call another method from the class in the constructor. Maybe it's a scope problem? I'm not sure... Thanks in advance for any help!
Code: Select all
<?php
class ParamsChecker {
function __construct($post, $get) {
CheckParams($post, $get);
}
public function CheckParams($post, $get) {
$containsData = 0;
$getParamsValid = 0;
$validParams = array('artist','album','genre','soundslike','mood');
if(isset($post['searchsubmit'])) { //Check if the submit button has been pressed
foreach($post as $key=>$value) { //If so, loop through the post array
if($key != 'searchsubmit') { //Ignore the submit button value, since we already know the submit button's been pressed
if($value = '') { //Check to see if the search text boxes contain any value
$containsData += 0; //If the search box is empty, add nothing to the $containsData variable, keeping its value false
} else {
$containsData++; //If the search box contains data, add 1 to $containsData, making it true
}
}
}
if($containsData) { //If any of the search boxes contain data, change $status so the code knows to show search results
$status = 'ShowSearchResults';
} else { //Otherwise, change $status so code knows to display an error message
$status = 'ShowErrorMessage';
}
} else if(isset($get['param'])) { //If the submit button hasn't been pressed, check to see if a $_GET variable exists
foreach($validParams as $value) { //Loop through the $_GET variable
if($get['param'] != $value) {
$getParamsValid += 0; //If the $_GET parameter doesn't contain a valid parameter name, keep $getParamsValid false
}
else {
$getParamsValid++; //Otherwise, make the $getParamsValid variable true
}
}
if($getParamsValid) {
$status = "ShowCategory";
} else {
$status = "DoNothing";
}
} else {
$status = "DoNothing"; //If the $_POST and $_GET variables are both empty, tell the script to do nothing
}
return $status;
}
}
?>Code: Select all
$status = new ParamsChecker($_POST, $_GET);
var_dump($status);
Code: Select all
$checker = new ParamsChecker();
$status = $checker->CheckParams($_POST, $_GET);