PHP-Object

Non dynamic base object class for PHP. Allows making getters and setters via get* and set* methods with public/protected accessor. Not defined properties will throw error.
Basically instead of shared magic methods __set, __get, __isset, __unset you can define individual methods for each property:
- get* - get[Property name from upper-case char], for reading property value.
- set* - set[Property name from upper-case char], for setting property new value.
- has* - has[Property name from upper-case char], for checking is property set.
- remove* - remove[Property name from upper-case char], for removing(using
unset() on it) property.
has* and remove* methods have default action and are optional. By default, has* will always return true for properties with defined getter and remove* will try to pass null into setter.
class MyObject extends \aw\Object {
private $_property = null;
public function getProperty(){
return 'GET-'.$this->_property;
}
public function setProperty($value){
$this->_property = 'SET-'.$value;
}
public function getData(){
return 'DATA:'.$this->hiddenProperty;
}
protected function getHiddenProperty(){
return 'hidden value';
}
}
Instances of MyObject will be non-dynamic objects with one read-write property property that can be accessed via $instance->property and two read-only properties -- hiddenProperty and its alias data.
$instance = new MyObject();
echo $instance->property.PHP_EOL;
$instance->property = 'something';
echo $instance->property.PHP_EOL;
echo $instance->getData().PHP_EOL;
echo $instance->data.PHP_EOL;
echo $instance->anyProperty.PHP_EOL;
You can change behaviour of your property when isset/unset are used via has* and remove* methods.
class StringProperty extends \aw\Object {
private $_property = '';
public function getProperty():string {
return $this->_property;
}
public function setProperty(string $value) {
$this->_property = $value;
}
protected function hasProperty():bool {
return (bool)$this->_property;
}
protected function removeProperty() {
$this->_property = '';
}
}
Then check if your property is empty or set it to be empty:
$prop = new StringProperty();
echo json_encode(isset($prop->property)).PHP_EOL;
$prop->property = 'value';
echo json_encode(isset($prop->property)).PHP_EOL;
unset($prop->property);
echo json_encode($prop->property).PHP_EOL;
echo json_encode(isset($prop->property)).PHP_EOL;
Note: Defining has* and remove* methods is optional, but without them you will not be able to define logic for isset() and unset() actions over your property. Without them unset/delete property via unset() just tries to pass null into property mutator method and even if property set to null, isset() will always return true:
class MySimpleObject extends \aw\Object {
private $_property = null;
public function getProperty(){
return $this->_property;
}
public function setProperty($value){
$this->_property = $value;
}
}
$simple = new MySimpleObject();
$simple->property = 'something';
echo 'Is set: '.json_encode(isset($simple->property)).PHP_EOL;
echo 'Is null: '.json_encode(is_null($simple->property)).PHP_EOL;
unset($simple->property);
echo 'Is set: '.json_encode(isset($simple->property)).PHP_EOL;
echo 'Is null: '.json_encode(is_null($simple->property)).PHP_EOL;