php class protected var1 foo_1 protected var2 foo_2 public function __

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
class A {
protected $var1 = 'foo_1';
protected $var2 = 'foo_2';
public function __call($name, $arg) {
$action = substr($name, 0, 3);
$var_name = strtolower(substr($name, 3));
if($action == 'get') {
return $this->__get($var_name);
} elseif($action == 'set') {
$this->__set($var_name, $arg[0]);
return 1;
} else {
/// Обработка ошибок
}
}
protected function __get($name) {
return $this->$name;
}
protected function __set($name, $value) {
$this->$name = $value;
}
}
$Object = new A();
echo $Object->getVar1().'<br />';
$Object->setVar1('Something else');
echo $Object->getVar1().'<br />';