An open API service indexing awesome lists of open source software.

https://github.com/simplecomplex/php-explorable

Foreach'ing protected members made simple
https://github.com/simplecomplex/php-explorable

Last synced: about 1 year ago
JSON representation

Foreach'ing protected members made simple

Awesome Lists containing this project

README

          

# (PHP) Explorable
composer namespace: simplecomplex/**explorable**

## Foreach'ing protected members made simple

An interface, base class and traits making it a breeze to expose a class'
protected members.

Extending `Explorable` facilitates:
- `isset()`
- `count()`
- `foreach (...`
- `var_dump()`
- `toObject()`, `toArray()`
- `json_encode()`

## Usage

### Class declaring it's properties
Property names hardcoded in constant `EXPLORABLE_VISIBLE`.
```php
true,
'bar' => true,
];

protected string $foo;

protected string $bar;
}
```

### Class relying on property table discovery
The properties will be discovered on-demand, via `explorablePrepare()`.

All instance vars must be nullable and declared as null
(`protected ?string $foo = null;`).

Otherwise risk of getting "_::$foo must not be accessed before initialization_"
error, or the instance vars simply won't get discovered (because not set to a
value (null)).

```php