PDA

View Full Version : PHP Classes



mobilebadboy
10-07-2007, 11:29 AM
I'm trying to get into OOP and was checking classes out. I found what I figured would be a simple first choice, which is a form validation class. It works, but it's spitting out a warning message telling me it's missing an argument, which is what the class is supposed to be checking for.

I know I could suppress it, but I'd rather know if anything can be done to fix it.


Warning: Missing argument 1 for validate() in /myscript/ on line 82

// The errored line
function validate($theinput,$description = ''){


My class:


class Validate {

var $errors;
function validate($theinput,$description = ''){
if (trim($theinput) != "") {
return true;
}else{
$this->errors[] = $description;
return false;
}
}
function foundErrors() {
if (count($this->errors) > 0){
return true;
}else{
return false;
}
}
function listErrors($delim = ' '){
return implode($delim,$this->errors);
}

}

Other code:


$validator = new Validate();

$validator->validate($_POST["user"],'Username');
$validator->validate($_POST["pass"],'Password');
if ( $validator->foundErrors() ){
echo 'The following fields were empty: <br />'.$validator->listErrors('<br />');

}else{
// rest of my code
}

Selkirk
10-07-2007, 02:49 PM
You are using php 4 style classes. I would highly recommend using php 5, if you can. Your problem is that the name of the class and your function name are the same. This is a special case in php 4 called a constructor. The constructor is called when the class is instantiated. So, the line


new validate()

actually calls the function validate() on the Validate class. Since you did not pass any parameters in the call, there is an error.

It is most unfortunate that PHP reports the error line number as the declaration of the class and not the call.

In PHP 5, there is a less confusing notation for declaring a constructor. All constructor methods use the name __construct. However, since there is still backward compatibility with 4, your case still causes the same error in php 4.

Change the name of your class to validator to fix the problem.

Selkirk
10-07-2007, 02:54 PM
I just ran this small test program:


<?php
function test($arg) { echo "talk like a pirate."; }
test();
?>

Which gives me the error message:

Warning: Missing argument 1 for test(), called in /Users/jeff/- on line 3 and defined in /Users/jeff/- on line 2

Which is much more informative. This was tested on php 5.2.2. Perhaps your error message from an older version of PHP is not so helpful? Another reason to upgrade.

mobilebadboy
10-07-2007, 03:16 PM
Thanks, for both the explanation and the easy fix. That did the trick and makes sense. I'm tired of coding stuff like if((!$_POST["user"] || !$_POST["pass"] ||...... that I want to learn how to do it different and better. Plus I should just learn OOP which I've put off for a while now.

It's funny, I did something similar about a week ago. Assigned $cat = $_GET["cat"] at the beginning of a script, further in the script had a MySQL query set up as $cat = mysql_query("select.......") and then couldn't figure out why any queries following that one quit working. Drove me nuts for like 20 minutes, then I felt like a dummy once I realized it.

I've thought often about updating to PHP5, but with so many script across so many domains, I keep putting it off in fear off a mass meltdown of my websites. Most of the scripts should survive, but it's still that "oh crap I have to stay up all night recoding..." thing in the back of my mind that gets me.