I am new to OOP and slowly starting to learn it to increase my PHP. This is my first attempt at writing something using OOP.
Now people always talk about separating the logic and php code from the HTML / views. This is a rather difficult concept for me to grasp thus, I tried making my own little MVC "framework" simply to fool around in an attempt to learn new concepts. I came up with the following.
CONTROLLER
require_once("../config/db.php");
class Employer
{
public $name, $location, $email;
private $password;
function setPword($newPword){
$this->password = $newPword;
}
function getPword(){
return $this->password;
}
function setEmployerName($newName)
{
$this->name = $newName;
}
function getEmployerName()
{
return $this->name;
}
function getEmployerFirstname(){
$fullname = $this->getEmployerName();
$fullname = explode(' ', $fullname);
return $firstname = $fullname[0];
}
function getEmployerLastName(){
$fullname = $this->getEmployerName();
$fullname = explode(' ', $fullname);
return $lastname = $fullname[1];
}
function setEmail($newEmail){
$this->email = $newEmail;
}
function getEmail(){
return $this->email;
}
public function isRegistered(){
global $db;
$email = $this->getEmail();
$sql="SELECT email FROM users WHERE email = :email";
$stmnt = $db->prepare($sql);
$stmnt->bindValue(":email", $email);
$stmnt->execute();
$stmnt->fetchAll();
if($stmnt->rowCount() > 0){
return die("Email Already Registered");
}
return '';
}
function registerNewEmployer(){
global $db;
try {
$firstname = $this->getEmployerFirstname();
$lastname = $this->getEmployerLastName();
$email = $this->getEmail();
$location = $this->getLocation();
$pword = $this->getPword();
$sql = "INSERT INTO users (firstname, lastname, email, pword, userType, joinDate )
VALUES
(:firstname, :lastname, :email, :pword, :userType, :joinDate)";
$stmnt = $db->prepare($sql);
$stmnt->bindValue(':firstname', $firstname);
$stmnt->bindValue(':lastname', $lastname);
//$stmnt->bindValue(':usernane', $username);
$stmnt->bindValue(':email', $email);
$stmnt->bindValue(':pword', $pword);
$stmnt->bindValue(':userType', '2');
$stmnt->bindValue(':joinDate', Date('Y-m-d')');
}
catch(Exception $e){
$e->getMessage("You could not be registered at this time!");
}
$stmnt->execute();
if ($stmnt->rowCount() > 0) {
return $success = "New Employer Successfully Registered";
} else {
return $error = "Registration Failed";
}
}
MODEL
<?php
$URL = $_POST['url'];
$page = explode('/', $URL);
if($page[3] == 'signup.php'){
require_once('../controller/users.php');
$obj = new employer();
$obj->setEmployerName($_POST['fullname']);
$obj->getEmployerFirstname();
$obj->getEmployerLastName();
$obj->setPword($_POST['pword']);
$obj->setEmail($_POST['email']);
echo $obj->registerNewEmployer();
?>
VIEW
<form role="form" id="registerForm" name="registerForm" method="post" action="../model/router.php">
<div class="form-group">
<label for="fullname" class="col-2 col-form-label">Fullname</label>
<div class="col-4">
<input type="text" name="fullname" required="required" id="fullname" class="form-control" placeholder="Enter Fullname" />
</div><!--/col4-->
</div><!--/form group-->
<div class="form-group">
<label for="email" class="col-2 col-form-label">Email</label>
<div class="col-4">
<input type="email" name="email" required id="email" class="form-control" placeholder="Enter Email Address" />
</div><!--/col4-->
</div><!--/formgroup-->
<div class="form-group">
<label for="pword" class="col-2 col-form-label">Password</label>
<div class="col-4">
<input type="password" name="pword" required="required" id="pword" class="form-control" placeholder="Enter Password" />
</div><!--/col4-->
</div><!--/form group-->
<div class="form-group">
<label for="pword2" class="col-2 col-form-label">Confirm Password</label>
<div class="col-4">
<input type="password" required id="pword2" name="pword2" class="form-control" placeholder="Confirm Password" />
</div><!--/col4-->
</div><!--/formgroup-->
<div class="form-group">
<input type="text" style="display: hidden" name="url" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />
<button class="btn btn-info btn-size form-control" name="submitBtn" id="submitBtn">Join</button>
</div><!--/col12-->
</form>
The code above is used for a basic registration form. Now all this is working fine however I have a few questions,
The Model, called router.php works as follows. The user submits the form it gets the page,
explode()
it and if the page name matches the page name hardcoded inside the router function it will process the PHP code derived from controller.The problem with this is since the page name is hardcoded in the router model
$page = explode('/', $URL) if($page[3] == 'signup.php'){}
if the page name changes it will result in an error. How can I improve this?Furthermore I would really appreciate if someone could have a look at my
registerNewEmployer()
method. Is this a semi-acceptable way to code a model in the OOP realm? How can above mentioned model be improved? (keeping in mind I am a beginner)Any other methods you see a problem with that needs improvement?
You will notice I use global
$db
inside my methods why is this considered a bad practice / what are the draw backs of doing this?
Any other errors problems / recommendations / advice will be enormously appreciated.