PHPackages                             inspirum/arrayable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. inspirum/arrayable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

inspirum/arrayable
==================

PHP arrayable interface for handling casting to an array

v1.3.0(1y ago)2128.6k↓34%12MITPHPPHP ^8.1CI passing

Since Jul 4Pushed 1mo agoCompare

[ Source](https://github.com/inspirum/arrayable-php)[ Packagist](https://packagist.org/packages/inspirum/arrayable)[ Docs](https://github.com/inspirum/arrayable-php)[ RSS](/packages/inspirum-arrayable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (5)Versions (6)Used By (2)

Arrayable
=========

[](#arrayable)

[![Latest Stable Version](https://camo.githubusercontent.com/493c58f700395ee6816946c3ee0a614a8698bf1c5f129244256f8f7a895386ce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e73706972756d2f617272617961626c652e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d626c7565)](https://packagist.org/packages/inspirum/arrayable)[![Build Status](https://camo.githubusercontent.com/f33b12800f3c13507cd92a25858be0279bc1ef07634ef25b31c6475bd42a4e4c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f696e73706972756d2f617272617961626c652d7068702f6d61737465722e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/inspirum/arrayable-php/actions)[![Coverage Status](https://camo.githubusercontent.com/38cbeff1a9c1c0b691ea8d0d1c411ca17671e47f576fe9c9b4eb5f34b1f57f8a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f696e73706972756d2f617272617961626c652d7068702f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/inspirum/arrayable-php/code-structure)[![Quality Score](https://camo.githubusercontent.com/a926344b131e57fc274bbafc210bc091f4be68ac81b231243b017a06b54fd1b4/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f696e73706972756d2f617272617961626c652d7068702e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/inspirum/arrayable-php)[![PHPStan](https://camo.githubusercontent.com/4b1330d67416a001f59bf39d60499aec28a71f8ee2d3185777cae563373ef201/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c652d6c6576656c25323031302d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265266c6162656c3d7068707374616e)](https://github.com/phpstan/phpstan)[![Total Downloads](https://camo.githubusercontent.com/365ae99cadbbb072a7dfdc942acb4d96b422aded52c4d36b2280b46d8cc6317a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696e73706972756d2f617272617961626c652e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d626c7565)](https://packagist.org/packages/inspirum/arrayable/stats)[![Software License](https://camo.githubusercontent.com/09d95b4610a1fd2497fcb38c18783f367895bddfbb68dddd9a8bb83c758630c5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696e73706972756d2f617272617961626c652d7068702e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d626c7565)](./LICENSE.md)

Motivation
----------

[](#motivation)

Unfortunately PHP does not have a nice way how to typecast objects to `array`.

There is the `__toString()` magic method for [`\Stringable`](https://www.php.net/manual/en/class.stringable.php) interface (since PHP 8.0) and the `jsonSerialize()` method for [`\JsonSerializable`](https://www.php.net/manual/en/class.jsonserializable.php) interface (since PHP 5.4), but `__toArray()` method is not (and will not) be supported – there are just several rejected draft RFC ([object\_cast\_to\_types](https://wiki.php.net/rfc/object_cast_to_types), [to array](https://wiki.php.net/rfc/to-array), ...) that suggests some kind of object to scalar type casting.

But so far (at least) there is no way to implement some (not even magic) method to be called when cast to `array`.

Ideally, something like this would work:

```
class Person
{
    public function __construct(
        public string $name,
        protected string $username,
        private string $password,
    ) {}

    public function __toArray(): array
    {
        return [
            'name' => $this->name,
            'email' => $this->username,
        ];
    }
}

$person = new Person('John Doe', 'j.doe@example.com', 'secret_pwd');

$personArray = (array) $person; // casting triggers __toArray()

/**
var_dump($personArray);
[
  'name' => 'John Doe'
  'email' => 'j.doe@example.com'
]
*/
```

but actually it cast to array like this:

```
/**
var_dump($personArray);
[
  'name' => 'John Doe'
  '*username' => 'j.doe@example.com'
  'Person@password' => 'secret_pwd'
]
*/
```

Usage example
-------------

[](#usage-example)

*All the code snippets shown here are modified for clarity, so they may not be executable.*

This package implements simple `\Arrayable` (or `\Inspirum\Arrayable\Arrayable`) interface.

```
/** @implements \Arrayable */
class Person implements \Arrayable
{
    public function __construct(
        public string $name,
        protected string $username,
        protected string $password,
    ) {}

    /** @return array */
    public function __toArray(): array
    {
        return [
            'name' => $this->name,
            'email' => $this->username,
        ];
    }
}

$person = new Person('John Doe', 'j.doe@example.com', 'secret_pwd');
```

There is `\is_arrayable()` function (or `\Inspirum\Arrayable\Convertor::isArrayable()` method) to check if given data are able to type cast itself to `array`.

```
var_dump(\is_arrayable([1, 2, 3])); // bool(true)
var_dump(\is_arrayable(new \ArrayIterator([1, 2, 3]))); // bool(true)
var_dump(\is_arrayable(new \ArrayObject([4, 5, 6]))); // bool(true)
var_dump(\is_arrayable((function () { yield 1; })())); // bool(true)
var_dump(\is_arrayable(1)); // bool(false)
var_dump(\is_arrayable(new \stdClass())); // bool(false)
var_dump(\is_arrayable(new class {})); // bool(false)
var_dump(\is_arrayable(new class implements \Arrayable {})); // bool(true)
var_dump(\is_arrayable($person); // bool(true)
```

Then there is `\to_array()` function (or `\Inspirum\Arrayable\Convertor::toArray()` method) to recursively cast data to `array`.

```
$data = \to_array(new \ArrayIterator([1, $person, (object) ['a' => true]]));

/**
var_dump($data);
[
  0 => 1
  1 => [
    'name' => 'John Doe'
    'email' => 'j.doe@example.com'
  ]
  2 => [
   'a' => true
  ]
*/
```

There is also helper abstract classes for common use for DAO ([`BaseModel`](./src/BaseModel.php)) and collection ([`BaseCollection`](./src/BaseCollection.php)) objects.

System requirements
-------------------

[](#system-requirements)

- [PHP 8.1+](http://php.net/releases/8_1_0.php)
- [ext-json](http://php.net/json)

Installation
------------

[](#installation)

Run composer require command

```
$ composer require inspirum/arrayable
```

Testing
-------

[](#testing)

To run unit tests, run:

```
$ composer test:test
```

To show coverage, run:

```
$ composer test:coverage
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](./docs/CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](./docs/CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Tomáš Novotný](https://github.com/tomas-novotny)
- [All Contributors](https://github.com/inspirum/arrayable-php/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](./LICENSE.md) for more information.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance65

Regular maintenance activity

Popularity35

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~198 days

Total

5

Last Release

620d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

v1.2.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/e2804b72261a28767b27deae892144e2a2c6a09a0d71d149631606346731d52d?d=identicon)[tomas-novotny](/maintainers/tomas-novotny)

---

Top Contributors

[![tomas-novotny](https://avatars.githubusercontent.com/u/36948723?v=4)](https://github.com/tomas-novotny "tomas-novotny (39 commits)")

---

Tags

arrayarrayablephpto-arraytoarraytoarray-interfacearrayableobject-to-array

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/inspirum-arrayable/health.svg)

```
[![Health](https://phpackages.com/badges/inspirum-arrayable/health.svg)](https://phpackages.com/packages/inspirum-arrayable)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
