0

I have created a class for user login and user register. They are quite similar, the only difference is that class login only creates a form for the user to input email and password, the password is then checked against the database whilst the register class gets the user to input email, password, firstname and lastname and writes these to the database.

For example here is my simple login class:

class Login {
private $email;
private $password;

private $server;
private $username;
private $database_password;
private $database;

function __construct( $server, $username, $database_password, $database) {
    $this->server = $server;
    $this->username = $username;
    $this->database_password = $database_password;
    $this->database = $database;

    $this->init('email');
    $this->init('password');
}

private function init($value) {
    if(isset($_POST[$value])) {
        $this->$value = $_POST[$value];
    }
    else {
        $this->$value = null;
    }
}

public function checkFormValues() {
    if($this->email== null || $this->password == null) {
        if($this->email != null || $this->password != null ) {
            echo "<p>Please fill out all the fields</p>";
        }

        echo "
        <p>Please complete the form below</p>
        <form action=\"user_login.php\" name=\"login\" method=\"post\">
            email    :<input type=\"text\" name=\"email\" value=$this->email ><br>
            password :<input type=\"password\" name=\"password\" value=$this->password ><br>
            <input type=\"submit\" value=\"submit\" />
        </form>
        ";
        }

    else {
        $mysqli = new mysqli( $this->server, $this->username, $this->database_password, $this->database );
        $db = $mysqli->select_db( $this->database );
        $query = "SELECT password FROM Users WHERE emailAddress='$this->email'";
        $users_password = $mysqli->query($query);
        $row = $users_password->fetch_row();

        if($row[0] == md5($this->password))
            echo "correct";

        else
            echo "incorrect";
    }

  } 
}

Below is my register class:

class Register {

private $firstname;
private $lastname;
private $email;
private $password;

private $server;
private $username;
private $database_password;
private $database;


function __construct( $server, $username, $database_password, $database ) {
    $this->server = $server;
    $this->username = $username;
    $this->database_password = $database_password;
    $this->database = $database;

    $this->init('firstname');
    $this->init('lastname');
    $this->init('email');
    $this->init('password');    

    }

private function init($value) {
    if(isset($_POST[$value])) {
        $this->$value = $_POST[$value]; 

    }
    else {
        $this->$value = null;
    }

}

public function checkFormValues() {
    if((($this->firstname == null) || ($this->lastname == null)) || (($this->email == null) || ($this->password == null))) {
        if((($this->firstname != null) || ($this->lastname != null)) || (($this->email != null) || ($this->password != null))) {
            echo "<p>Please fill out all the fields</p>";
        }

        echo "
        <p>Please complete the form below</p>
        <form action=\"register_class.php\" name=\"register\" method=\"post\">
            First Name:<input type=\"text\" name=\"firstname\" value=$this->firstname ><br >
            Last Name: <input type=\"text\" name=\"lastname\" value=$this->lastname ><br>
            email    :<input type=\"text\" name=\"email\" value=$this->email ><br>
            password :<input type=\"password\" name=\"password\" value=$this->password ><br>
            <input type=\"submit\" value=\"submit\" />
        </form>
        ";
        }

    else{

    $mysqli = new mysqli( $this->server, $this->username, $this->database_password, $this->database );
    $db = $mysqli->select_db( $this->database );
    $query ="INSERT INTO Users(firstName, lastName, emailAddress, password) VALUE('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['email']."', '".md5($_POST['password'])."')"; 
    $mysqli->query($query);
    $mysqli->close();
    }
}

}

In this case would it be a good idea to use an abstract class. I'm new to OOP and read up on abstract classes on php.net. If it is not suitable is there anything I should do to avoid having two classes with essentially the same code. Thank you for your help.

1 Answer 1

0

Yes but your code is far from an object oriented design becouse your classes are doing to much:

  • handling CRUD operations
  • using global variables
  • creating HTML output

Right now your UI has a hard dependency on a MySQL database but why is that?

You should read and learn more about what OO means, for example the SOLID principles.

2
  • Ok thanx for your input. Would it be better If I split this into several classes? If so what would you recommend? Mar 8, 2014 at 20:29
  • Yes it would be better but i can only show you some main guidelines like put you database connection stuff in a separated claas and use PDO, put you HTML code into a view template and so on. Fox a complete solution we need to know the whole infrastructure you are using and you need to dive in OO programming.
    – Peter Kiss
    Mar 9, 2014 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.