I am trying to verify whether the method in my class is returning a true value. Please look at my object below the class and tell me if it is a valid statement. I am using this to verify whether an email address already exists in the database.
My class and it’s constructor
class CheckEmail {
public function __construct($email) {
$db = Database::GetHandler();
$sql = "SELECT email from users WHERE email='$email'";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows = $stmt->rowCount();
if($rows > 0) {
return true;
} else {
return false;
}
}
}
My object from this class:
if($checkEmail = new CheckEmail($_POST[email])==true) {...
Constructors cannot return a value, that doesn’t make any sense. Constructors are there to create (and return) an object of its class.
You should make another function to do this check, and then call that.
(P.S. You can just do
return $rows > 0;)And then you can call it like this:
Thing is, do you really need a class here? You could just declare the
CheckEmailfunction normally, and not in its own class.