Question #30408

Provide and explain two examples where you would utilize immutable classes?

Expert's answer

class Name { // Announce the class
private $val; // Announce the variable
public function __construct() {
// Standard value
$this->val = "qwerty";
}
public function __set($key, $val) {
// Do nothing when you try to change smth.
return;
}
public function __get($key) {
// Check if the attribute exists in this class
if (property_exists($this, $key)) {
// Then return it
return $this->{$key};
} else {
return null;
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS