https://github.com/burdiuz/php-object
Non dynamic base object class for PHP
https://github.com/burdiuz/php-object
Last synced: 9 months ago
JSON representation
Non dynamic base object class for PHP
- Host: GitHub
- URL: https://github.com/burdiuz/php-object
- Owner: burdiuz
- License: mit
- Created: 2015-11-22T15:42:31.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-06T07:58:26.000Z (almost 10 years ago)
- Last Synced: 2025-03-05T17:53:35.401Z (10 months ago)
- Language: PHP
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## PHP-Object
[](https://travis-ci.org/burdiuz/php-object)
[](https://coveralls.io/github/burdiuz/php-object?branch=master)
[](https://www.versioneye.com/user/projects/568a56eceb4f47003c001007)
[](https://packagist.org/packages/actualwave/object) [](https://packagist.org/packages/actualwave/object) [](https://packagist.org/packages/actualwave/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.
### Installation
Via [composer](https://getcomposer.org/)
```
composer require actualwave/object
```
### Usage
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.
```php
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`.
```php
$instance = new MyObject();
echo $instance->property.PHP_EOL; // GET-
$instance->property = 'something';
echo $instance->property.PHP_EOL; // GET-SET-something
echo $instance->getData().PHP_EOL; // DATA:hidden value
echo $instance->data.PHP_EOL; // DATA:hidden value
echo $instance->anyProperty.PHP_EOL; // throws error Property accessor "anyProperty" not found.
```
You can change behaviour of your property when `isset`/`unset` are used via `has*` and `remove*` methods.
```php
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:
```php
$prop = new StringProperty();
echo json_encode(isset($prop->property)).PHP_EOL; // false
$prop->property = 'value';
echo json_encode(isset($prop->property)).PHP_EOL; // true
unset($prop->property);
echo json_encode($prop->property).PHP_EOL; // "" -- will output empty string in JSON format
echo json_encode(isset($prop->property)).PHP_EOL; // false
```
*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`:
```php
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; // Is set: true
echo 'Is null: '.json_encode(is_null($simple->property)).PHP_EOL; // Is null: false
unset($simple->property);
echo 'Is set: '.json_encode(isset($simple->property)).PHP_EOL; // Is set: true
echo 'Is null: '.json_encode(is_null($simple->property)).PHP_EOL; // Is null: true
```