Savybių tikrinimas rašymo metu OOP PHP
Patikrinkime savybių reikšmes
ar atitinka tam tikras sąlygas
naudodami metodą __set:
<?php
class Test
{
private $prop1;
private $prop2;
public function __set($property, $value)
{
switch($property) {
case 'prop1':
if ($value > 0 and $value < 10) {
$this->$property = $value;
}
break;
case 'prop2':
if ($value > 10 and $value < 20) {
$this->$property = $value;
}
break;
default:
echo 'Savybė neegzistuoja';
break;
}
}
public function __get($property)
{
return $this->$property;
}
}
?>