class AbcModel {
private $property1; // Anything required internally, like $userCollection etc
public $property2; // Anything which controller can directly see and change
function __construct() {
// this should call constructors based on param count
// Refer to : http://php.net/manual/en/language.oop5.decon.php#99903
}
// followed by constructors
public function save() {
// saves the model into db
}
public function retrieve() {
// gets the model from db using $email or $_id, either of which would be set
// in case of UserModel. NOTE : no params
}
public function exists() {
// boolean based function
}
public function delete() {
// deletes using $email or $_id, either of which would be set in case of UserModel
}
public function create($array_of_details_of_the_model_to_be_saved) {
// saves on the basis of the given array, throws exception in
// case of error and finally fills the properties of the class for use
}
public function set($array_of_details) {
// Takes the array and sets all the public fields like name etc, if present in the array
}
}
$user = new UserModel($email, $pass); // This constructor would authenticate, or throw exception
echo $user->name; // This should give the name (no need to retrieve user)
$user = new UserModel($email); // This constructor should fetch user details
// but hide sensitive data like pass. Such a model is immutable,
// ie we can only read but not write anything in it.
echo $user->email; // This should give the email of user
$user = new UserModel(); // An empty user model is created
$user->create([
"email" => "[email protected]",
"password" => "unhashed password",
"name" => "Some One",
"gender" => "male"
]); // Creates a new model and saves it.
// This should verify , hash the password and create a user.
$user = new UserModel($email, $pass);
$user->name = "Someone Else";
$user->save(); // This should save the above made change
$user = new UserModel($email); // Immutable object
$user->name = "Someone Else";
$user->save(); // This should throw an exception as the model
// isn't authenticated to make such changes.