0

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) {...
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
stevenpepe
  • 267
  • 4
  • 16
  • 2
    That's not how OOP is supposed to work. If you want a function which returns `true` or `false`, then create a function. I don't know what PHP is doing when you are not returning an object from the constructor, but even if it was returning the boolean, it's a bad design. – Felix Kling Mar 15 '12 at 18:43
  • 2
    You are **wide open** to SQL injection. Learn to do a proper prepared query with PDO, or **you will be hacked** if you haven't been already. – Brad Mar 15 '12 at 18:44
  • 2
    possible duplicate of [Echo Return construct method;](http://stackoverflow.com/questions/3254279/echo-return-construct-method), [Is it ok for a construct to return something?](http://stackoverflow.com/questions/6821288/is-it-ok-for-a-construct-to-return-something) – jprofitt Mar 15 '12 at 18:44
  • 1
    @Brad, why do you assume I don't know prepared statements just because this method is not written with one. I would've preferred if you answered the question constructively, rather than making assumptions. – stevenpepe Mar 15 '12 at 18:59
  • 1
    @stevenpepe, It is a very safe assumption that anyone who knew the dangers (and simple problems) that come along with SQL statements like the one you have written would never write their code that way. I'm trying to help you, and if you don't want my advice then that is certainly your prerogative. – Brad Mar 15 '12 at 19:06

3 Answers3

4

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.

class CheckEmail {

    public function check($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;
        }
    }
}

(P.S. You can just do return $rows > 0;)

And then you can call it like this:

var $email = new CheckEmail;
if($email->check($_POST[email]) === TRUE){
// or just if($email->check($_POST[email])){

Thing is, do you really need a class here? You could just declare the CheckEmail function normally, and not in its own class.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Thank you Rocket. This works fine. No, the class is overkill. I am using this as an example of how to return the value from a class method. – stevenpepe Mar 15 '12 at 18:56
3

Constructors do not return a value. You need a different approach. http://www.php.net/manual/en/language.oop5.decon.php

Cerad
  • 48,157
  • 8
  • 90
  • 92
-3

Try putting

$checkEmail = new CheckEmail($_POST[email]);

Before the if statement, then

If($checkEmail) {...
bts
  • 106
  • 1
  • 3
  • 1
    `$checkEmail` will be an object and not a boolean value, since constructors do not return anything except the object they created. – Jeff Lambert Mar 15 '12 at 18:55
  • You cannot return a value from a constructor. Also `if($var = function())` is valid; the `=` operator will return the value it just set. – gen_Eric Mar 15 '12 at 18:55