Semakan Sifat semasa Penulisan dalam OOP dalam PHP

Mari kita semak nilai sifat untuk memastikan ia mematuhi syarat tertentu menggunakan kaedah __set:

Test.php<?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 'property not exists';
        break;
      }
    }
    
    public function __get($property)
    {
      return $this->$property;
    }
  }
?>