class Object {
public function getClass () {
return get_class ($ this );
}
public function __get ($ name) {
$ getterMethod = 'get' . $ name;
if (method_exists ($ this , $ getterMethod)) {
return $ this -> $ getterMethod ();
} elseif (property_exists ($ this , $ name)) {
$ backtrace = debug_backtrace ();
trigger_error (
sprintf ( 'Cannot access the protected property% s :: $% s in <b>% s </ b> on line <b>% s </ b>' , $ this -> getClass (), $ name, $ backtrace [0] [ 'file' ], $ backtrace [0] [ 'line' ])
, E_USER_ERROR);
} else {
$ backtrace = debug_backtrace ();
trigger_error (
sprintf ( '<b> Notice </ b>: Undefined property:% s :: $% s in <b>% s </ b> on line <b>% s </ b>' , $ this -> getClass (), $ name, $ backtrace [0] [ 'file' ], $ backtrace [0] [ 'line' ])
, E_USER_ERROR);
}
return NULL;
}
public function __set ($ name, $ value ) {
$ setterMethod = 'set' . $ name;
if (method_exists ($ this , $ setterMethod)) {
$ this -> $ setterMethod ($ value );
} elseif (property_exists ($ this , $ name)) {
$ backtrace = debug_backtrace ();
trigger_error (
sprintf ( 'Cannot access the protected property% s :: $% s in <b>% s </ b> on line <b>% s </ b>' , $ this -> getClass (), $ name, $ backtrace [0] [ 'file' ], $ backtrace [0] [ 'line' ])
, E_USER_ERROR);
} else {
$ backtrace = debug_backtrace ();
trigger_error (
sprintf ( '<b> Notice </ b>: Undefined property: <b>% s </ b> :: $ <b>% s </ b> in <b>% s </ b> on line <b >% s </ b> ' , $ this -> getClass (), $ name, $ backtrace [0] [ ' file ' ], $ backtrace [0] [ ' line ' ])
, E_USER_ERROR);
}
}
} * This source code was highlighted with Source Code Highlighter .
class Rectangle extends Object {
protected $ width = 400;
protected $ height = 1;
protected function getWidth () {
return $ this -> width;
}
protected function getHeight () {
return $ this -> height;
}
public function setHeight ($ value ) {
$ this -> height = $ value ;
}
protected function getArea () {
return $ this -> width * $ this -> height;
}
}
$ rect = new Rectangle ();
echo $ rect-> width. '*' . $ rect-> height. '=' . $ rect-> area. "<br /> \ n" ;
$ rect-> height = 1000;
echo $ rect-> width. '*' . $ rect-> height. '=' . $ rect-> area. "<br /> \ n" ;
$ rect-> area = 500; * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/39338/
All Articles