note 61724 deleted from language.oop5 by nlopess

From: Date: Tue, 18 Apr 2006 17:40:16 +0000
Subject: note 61724 deleted from language.oop5 by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-108423@lists.php.net to get a copy of this message
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 ?>

« previous php.notes (#108423) next »