note 61724 added to language.oop5

From: Date: Fri, 10 Feb 2006 11:38:14 +0000
Subject: note 61724 added to language.oop5
Groups: php.notes 
Request: Send a blank email to php-notes+get-103429@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 64.246.30.37 Probable Submitter: 81.208.98.194 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61724 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61724&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61724&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103429) next »