List Info

Thread: note 61724 added to language.oop5




note 61724 added to language.oop5
user name
2006-02-10 11:38:14
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 -- htt
p://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.p
hp.net/manage/user-notes.php?action=delete+61724&report=
yes&reason=useless
Delete: non-english         -- http://mast
er.php.net/manage/user-notes.php?action=delete+61724&rep
ort=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://ma
ster.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

note 61724 deleted from language.oop5 by nlopess
user name
2006-04-18 17:40:16
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

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )