Somethings I found interesting for PHP OOP
Having OOPed in PHP5 for a while, I have some things that PHP5 have made more convinient than other OOP supported languages. One is the variable variables feature of PHP to make the setters and getters.
for example, a getter could look like the following:
public function get($att){
return $this->{$att};
}
That is, instead of makings lots of get_att, just make one function to return the fields.
By checking specific type, fields can be made private.
public function get($att){
if ($att!="password"){
return $this->{$att};
} else {
throw new Execption("Restricted Property Cannot Be Returned");
}
}
And a client could be called as
try { echo $object->get($att);
} catch .....execption handling..etc.
as proper calls are made, the right field will be printed otherwise exceptions will be caught. Adding more(lots) if statements for different attributes may result the code running at a insignificantly slower speed but it may simply lose the purpose of doing it using variable variables. Similarly, a setter can look like the following:
public function set($att,$new_value){
$new_value=$db->escape($new_value);
$att=$db->escape($new_value);
$this->db->query("UPDATE `table` SET '$att'='$new_value' WHERE `id`='$id'");
$this->{$att}=$new_value;
}
In Java, a map will simply be used. -> well,~it is much less convinent than as in php*.*Credit to Professor Sable.