Дон Карлос (kastaneda) wrote,
  • Music: Dvar «Fayah!» (2009) — Oramah Elahar
Чёртов Propel генерирует тонны кода, в котором довольно большую часть занимают getter'ы и setter'ы. Для сущности со свойствами foo и bar генерируется класс BaseSomething с предопределёнными методами getFoo, setFoo, getBar и setBar, которые можно (по мере необходимости) переопределять в «нормальном» классе Something. Это очень дубово, ни разу не по-PHPшному.

Я рисую (JFF) для себя небольшой ORM, и для getter'ов/setter'ов применяю простой хак:

abstract class container
{
    public function 
__get($name) {
        
$method 'get_'.$name;
        if( 
method_exists($this$method) ) {
            return 
$this->$method();
        }
        throw new 
Exception('Cannot get '.get_class($this).'::'.$name);
    }

    public function 
__set($name$value) {
        
$method 'set_'.$name;
        if( 
method_exists($this$method) ) {
            
$this->$method($value);
            return;
        }
        throw new 
Exception('Cannot set '.get_class($this).'::'.$name);
    }
}


Дальше всё очевидно. В модели пишу только те getter'ы и setter'ы, которые действительно нужны (вроде проверки корректности данных). Остальные свойства объявляю public'ом и не парюсь.

class something extends container
{
    public 
$foo;
    protected 
$bar// read-only

    
function __construct($foo$bar) {
        
$this->foo $foo;
        
$this->bar $bar;
    }

    function 
get_bar() {
        return 
$this->bar;
    }
}

$test = new something('a''b');
echo 
$test->foo// 'a'
echo $test->bar// 'b'
$test->bar 'c'// exception
Tags: webdev
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded  

  • 4 comments