(no subject)

Tuesday, January 26th, 2010 02:26
kastaneda: (Default)
[personal profile] kastaneda
Чёртов 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
(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

September 2025

M T W T F S S
12345 67
891011121314
15161718192021
22232425262728
2930