note 106460 added to language.oop5.visibility
| From: | briank at kappacs dot com | Date: | Thu, 10 Nov 2011 20:20:08 +0000 |
| Subject: | note 106460 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184380@lists.php.net to get a copy of this message | ||
To make (some) object members read-only outside of the class (revisited using PHP 5 magic __get):
<?php
class ReadOnlyMembers {
private $reallyPrivate;
private $justReadOnly;
function __construct () {
$this->reallyPrivate = 'secret';
$this->justReadOnly = 'read only';
}
function __get ($what) {
switch ($what) {
case 'justReadOnly':
return $this->$what;
default:
# Generate an error, throw an exception, or ...
return null;
}
}
function __isset ($what) {
$val = $this->__get($what);
return isset($val);
}
}
$rom = new ReadOnlyMembers();
var_dump($rom->justReadOnly); // string(9) "read only"
$rom->justReadOnly = 'new value'; // Fatal error
var_dump($rom->reallyPrivate); // Fatal error
?>
----
Server IP: 64.71.164.2
Probable Submitter: 99.114.241.185
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/106460
Del: integrated -- https://master.php.net/note/delete/106460/integrated
Del: useless -- https://master.php.net/note/delete/106460/useless
Del: bad code -- https://master.php.net/note/delete/106460/bad+code
Del: spam -- https://master.php.net/note/delete/106460/spam
Del: non-english -- https://master.php.net/note/delete/106460/non-english
Del: in docs -- https://master.php.net/note/delete/106460/in+docs
Del: other reasons-- https://master.php.net/note/delete/106460
Reject -- https://master.php.net/note/reject/106460
Search -- https://master.php.net/manage/user-notes.php