@ 2014-01-17T15:35:22Z <?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 && $this->oldemail == 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();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 INSERT INTO users (name, occupation, email) VALUES ('Luke', 'Programmer', 'luke@gmail.com')INSERT INTO users (name, occupation, email) VALUES ('Luke', 'Programmer', 'demo@example.co.za') Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 INSERT INTO users (name, occupation, email) VALUES ('Luke', 'Programmer', 'luke@gmail.com')
Fatal error: Call to undefined function filter_var() in /in/mPJiK on line 86
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/mPJiK on line 4
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/mPJiK on line 4
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/mPJiK on line 4
Process exited with code 255 . preferences:dark mode live preview
249.39 ms | 401 KiB | 385 Q