📜 ⬆️ ⬇️

Getters / Setters getting real

I have seen quite a lot of different options for implementing features that exist in ordinary OO languages ​​(Delphi, Java, ActionScript) - and which are often not enough php. Namely get & set methods.

In this topic, I propose a very simple and effective implementation that does not contain anything superfluous and at the same time perfectly implements get / set functionality that allows you to hide the variables themselves and at the same time make them read only, write only || read-write.

Further more
')
Source code:

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 .


Thus, the class that inherits this data - automatically gets the functionality
use get / set methods, while there is no need to redefine any methods in inheriting classes.

Of the benefits




Of the minuses




Example:



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 .


In the example above, you can see that the width property is read-only, while
The “height” of read-write + can be set in two ways.
And there is another property "area" (area), which is virtual, but at the same time can be called as $ rect-> area;

PS Here is my first "brick" in Habré :)

Source: https://habr.com/ru/post/39338/


All Articles