Finally, I’m getting into OOP and attempting to put together my first class. However, my IDE (NetBeans) throws up an error on the last function’s (sanitize()) closing parentheses. This doesn’t happen when all the class contents are extracted from the class.
Here’s my code:
class db_class {
$error_log = false;
function connect($db_host, $db_user, $db_pass) {
$connect = mysql_connect($db_host, $db_user, $db_pass);
return $connect;
}
function select_db($db_database) {
$select_db = mysql_select_db($db_database);
return $select_db;
}
function array_prepare($data) {
$size = sizeof($array);
foreach($array as $k => $v) {
$keys[] = $k;
}
$size_md = sizeof($array[$keys[0]]);
for($i = 0; $i < $size_md; $i++) {
$value .= '(';
for($j = 0; $j < $size; $j++) {
$value .= "'".$array[$keys[$j]][$i]."',";
}
$value = rtrim($value, ',');
$value .= '), ';
}
$value = rtrim($value, ',');
return $value;
}
function sanitize($data) {
if(is_array($data))
foreach($data as $k => $v)
$data[$k] = sanitize($v); //recursive
elseif(is_string($data))
$data = mysql_real_escape_string($data);
return $data;
} // <- where the error is thrown up
}
I really don’t see what I’ve done wrong, and the suggestions offered by NetBeans are many (it offers like 10 explanations for the syntax error). Can anyone pick up what could be causing this issue?
Any help would be greatly appreciated!
You’re missing a visibility declaration on your property.
In other answers they have you use
var, however I would advise you to use PHP5 visibility declarations;public,protectedorprivate. The visibility of the variable should be based on the need for that piece of data. As a general rule though, you should limit the amount of properties that are declaredpublic. Ultimately, you have no control over the data that can be assigned to apublicproperty.Furthermore, you should put a visibility declaration on your functions as well. Not having any visibility declaration will mark them as
public, this is probably not what you’ll want for all functions in a class.