Note Submitter: denis (dot) edistar.com
----
A trick to define readonly properties of class:
You define the readonly property as private and use the
magic function __get to return its value
<?php
class a {
private $readonlyProperty = 5;
private function __get($property) {
if($property == "readonlyProperty") {
return $this->$property;
)
else {
raise new Exception("No such property:
$property");
}
}
}
$example = new a();
echo $example->readonlyProperty; // this prints 5
$example->readonlyProperty = 10; // this prints Fatal
error: Cannot access private property readonlyProperty
?>
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|