note 71383 modified in language.oop5.overloading by danbrown
| From: | danbrown@php.net | Date: | Fri, 14 Nov 2008 17:15:07 +0000 |
| Subject: | note 71383 modified in language.oop5.overloading by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146543@lists.php.net to get a copy of this message | ||
example for read only properties in class object. Lets you manage read only properties with var
names like $ro_var.
The property must be PRIVATE, otherwise the overload method __get doesn't be called.
<?php
class Session {
private $ro_usrName;
function __construct (){
$this->ro_usrName = "Marcos";
}
function __set($set, $val){
if(property_exists($this,"ro_".$set))
echo "The property '$set' is read only";
else
if(property_exists($this,$set))
$this->{$set}=$val;
else
echo "Property '$set' doesn't exist";
}
function __get{$get}{
if(property_exists($this,"ro_".$get))
return $this->{"ro_".$get};
else
if(property_exists($this,$get))
return $this->{$get};
else
echo "Property '$get' doesn't exist";
}
}
?>
--was--
example for read only properties in class object. Lets you manage read only properties with var
names like $ro_var.
The property must be PRIVATE, otherwise the overload method __get doesn't be called.
class Session {
private $ro_usrName;
function __construct (){
$this->ro_usrName = "Marcos";
}
function __set($set, $val){
if(property_exists($this,"ro_".$set))
echo "The property '$set' is read only";
else
if(property_exists($this,$set))
$this->{$set}=$val;
else
echo "Property '$set' doesn't exist";
}
function __get{$get}{
if(property_exists($this,"ro_".$get))
return $this->{"ro_".$get};
else
if(property_exists($this,$get))
return $this->{$get};
else
echo "Property '$get' doesn't exist";
}
}
http://php.net/manual/en/language.oop5.overloading.php