@Conradaek - Here's the code for my model (cut down, but the rest is completely irrelevant to my issue):
class Users extends \Phalcon\Mvc\Model {
// These are the fields in my database.
private $id;
public $username;
public $email;
private $password;
public function getId() {
return $this->id;
}
// Should take care of hashing the new password.
public function setPassword($password) {
$this->password = $this->hashPassword($password); // This method exists in my real code, omitted from the example
}
public function getPassword() {
return $this->password;
}
}
As you can see, id
and password
are private. I have provided as my desire is for id
to be immutable, I have provided a getter and no setter. As password
must be hashed behind the scenes, I have made a setter that takes care of hashing.
@quasipickle - I understand your example. It is hack-ish, but if it comes to that then I guess I must do it. However, I have one question about your method. - These private variables are the fields in my database... wouldn't giving them a prefix screw with the saving of their values? And then I would have to create a hacky work around for that?
What blows my mind is that this functionality would even be allowed.
If I do $user->id = 95;
, it simply works. That defeats the entire purpose of a private variable.
However, if I do this: echo $user->id;
, I get the expected result of:
Notice: Access to undefined property PrivateMessages::id in C:\xampp\htdocs\bbg-phalcon\app\controllers\IndexController.php