<?php
class UserModel {
public $name = null, $occupation = null, $email = null, $oldname = null, $oldoccupation = null, $oldemail = null, $me, $handler, $result;
public function __construct(){
}
public function create($fields = array()){
if(count($fields) == 3){
$this->name = $fields['name'];
$this->occupation = $fields['occupation'];
$this->email = $fields['email'];
}
}
public function _save(){
if($this->oldname == null && $this->oldoccupation == null){
$sql = "INSERT INTO users (name, occupation, email) VALUES ('".$this->name."', '".$this->occupation."', '".$this->email."')";
/*$this->result = $this->handler->prepare($sql);
/$this->result->execute(array(
':name'=>$this->name,
':occupation'=>$this->occupation,
':email'=>$this->email
));*/
echo $sql;
} else {
$sql = "UPDATE users SET name = '".$this->name."', occupation = '".$this->occupation."', email = '".$this->email."' WHERE name = '".$this->oldname."'";
/*$this->result = $this->handler->prepare($sql);
$this->result->execute(array(
':name'=>$this->name,
':occupation'=>$this->occupation,
':email'=>$this->email,
':oldname'=>$this->oldname
));*/
echo $sql;
}
}
public function name($given_name = null) {
if($this->name == null){
if($given_name != null){
$this->name = $given_name;
}
} else {
if($given_name != null){
$this->oldname = $this->name;
$this->name = $given_name;
}
}
return $this->name;
}
public function occupation($given_occupation = null) {
if($this->occupation == null){
if($given_occupation != null){
$this->occupation = $given_occupation;
}
} else {
if($given_occupation != null){
$this->oldoccupation = $this->occupation;
$this->occupation = $given_occupation;
}
}
return $this->occupation;
}
public function email($given_email = null){
$this->verifyEmail($given_email);
if($given_email != null){
// $this->oldemail = $this->email; // THIS LINE IS THE ISSUE
$this->email = $given_email;
}
return $this->email;
}
public function verifyEmail($givenemail = null){
if($givenemail == null){
$email = $this->email;
} else {
$email = $givenemail;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Email not valid.');
die();
}
}
}
$user = new UserModel();
$user->create(array(
'name' => 'Luke',
'occupation' => 'Programmer',
'email' => 'luke@gmail.com'
));
$user->_save();
// $user->name('Jack');
// $user->occupation();
try {
$user->email('demo@example.co.za');
} catch (Exception $e) {
echo $e->getMessage();
}
$user->_save();
preferences:
29.72 ms | 402 KiB | 5 Q