PHPackages                             imponeer/toarray-interface - 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. imponeer/toarray-interface

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

imponeer/toarray-interface
==========================

A PHP interface defining a standard method for converting objects to arrays. Ensures consistent method naming and return type for array conversion across different classes.

v2.1.0(1y ago)516.1kMITPHPPHP &gt;=8.3CI passing

Since Mar 22Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/imponeer/toarray-interface)[ Packagist](https://packagist.org/packages/imponeer/toarray-interface)[ Docs](https://github.com/imponeer/toarray-interface)[ RSS](/packages/imponeer-toarray-interface/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (2)Versions (13)Used By (0)

[![License](https://camo.githubusercontent.com/4493e9fa9ed1ff4c5194b9bf2999c1ba7d57f7df9225e6ac3b8992ca9793af61/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696d706f6e6565722f746f61727261792d696e746572666163652e7376673f6d61784167653d32353932303030)](LICENSE)[![GitHub release](https://camo.githubusercontent.com/b64d6125c40b037c7b5304733a3249f45bd11d428ee705fcab639561fd66c7f5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f696d706f6e6565722f746f61727261792d696e746572666163652e7376673f6d61784167653d32353932303030)](https://github.com/imponeer/toarray-interface/releases) [![PHP](https://camo.githubusercontent.com/b56eb2729005bad6f56fc7589888e5371cbdd615159c9736c3c17b664238e158/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f696d706f6e6565722f746f61727261792d696e746572666163652e737667)](http://php.net)[![Packagist](https://camo.githubusercontent.com/ec0617641787e0de00e8a61bcf1e73ba64bbdd0bb4a1604a3a41baa314803c79/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f696d706f6e6565722f746f61727261792d696e746572666163652e737667)](https://packagist.org/packages/imponeer/toarray-interface)

ToArray Interface
=================

[](#toarray-interface)

There have been several attempts to introduce a standard `toArray` method or interface in PHP:

- The first attempt was in 2012, when an [RFC proposed adding several magic methods to PHP, including `__toArray`, `__toInt()`, `__toFloat()`, `__toScalar()`, and `__toBool()`](https://wiki.php.net/rfc/object_cast_to_types). This proposal failed before reaching the voting stage due to concerns about implementation details and lack of consensus.
- In 2019, a separate [RFC specifically focused on introducing only a `__toArray` method for objects](https://wiki.php.net/rfc/to-array). Unlike the broader 2012 proposal, this RFC aimed to standardize array conversion alone, but it also failed before reaching the voting stage due to lack of support and unresolved concerns—particularly that the `__toArray` method did not specify what kind of array should be returned (e.g., associative or indexed), nor whether nested arrays or objects were allowed as values. One of the main unresolved questions was how to specify or standardize the format of the returned array in a way that would suit all use cases.
- Over the years, various frameworks and libraries (such as [Laravel's illuminate/contracts](https://packagist.org/packages/illuminate/contracts)) have introduced their own `toArray` conventions, but these are not universal and often come with additional dependencies.
- As of now, neither PHP itself nor the [PHP-FIG group](https://www.php-fig.org/psr/) has adopted an official standard for object-to-array conversion.

This package was created to address this gap: it provides a minimal Composer package containing only the `ToArrayInterface`. Use it when you need to ensure an object can be converted to an array via a `toArray` method, without introducing unnecessary dependencies.

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

[](#installation)

To install and use this package, use [Composer](https://getcomposer.org):

```
composer require imponeer/toarray-interface
```

Alternatively, manually include the files from the `src/` directory.

**Note:** For PHP 5 projects, use [version 1.0 of this library](https://packagist.org/packages/imponeer/toarray-interface#1.0.0).

Example
-------

[](#example)

```
use Imponeer\ToArrayInterface;

class DummyObject implements ToArrayInterface {

    /**
     * Converts object to array
     *
     * @return array
     */
    public function toArray(): array {
        return [
            'hash' => sha1(time()),
        ];
    }
}

$instance = new DummyObject();
if ($instance instanceof ToArrayInterface) {
    var_dump($instance->toArray());
}
```

Documentation
-------------

[](#documentation)

This repository features an automatically updated [GitHub Wiki](https://github.com/imponeer/toarray-interface/wiki) containing up-to-date code documentation generated from the source. You can use the Wiki to explore interfaces, methods, and other technical details about the project.

Alternatives
------------

[](#alternatives)

Because PHP does not yet have a standard way to convert objects to arrays, many alternative libraries exist, each trying to solve the same problem in its own way. Depending on your specific needs - such as recursion support, fluent APIs, strict interface contracts, or framework integration - you may find one of these packages better suited for your project.

Below is a curated list of popular alternatives, showcasing different approaches to object-to-array conversion in PHP:

PackagePHP VersionFeatures[inspirum/arrayable](https://github.com/inspirum/arrayable-php)[![PHP](https://camo.githubusercontent.com/e30b6bb5d0e1574d808aae776e5ca3c115e35ff4229815868d365723efb39a98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f696e73706972756d2f617272617961626c652f706870)](http://php.net)Recursive conversion, helper functions, base classes[dmytrof/array-convertible](https://github.com/dmytrof/ArrayConvertible)[![PHP](https://camo.githubusercontent.com/b61547c966b86127db5db66960cec0a07ae8b79a16ec5bb138742a95aae2f3c9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f646d7974726f662f61727261792d636f6e7665727469626c652f706870)](http://php.net)Lightweight, nested conversion, simple interface[rexlabsio/array-object-php](https://github.com/rexlabsio/array-object-php)[![PHP](https://camo.githubusercontent.com/7bce390376937980460612d8b5b97c5c16335ca6539278288f1fed8dc1c99147/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7265786c6162732f61727261792d6f626a6563742f706870)](http://php.net)Fluent interface, `toArray()` and `toJson()` methods[php-extended/php-arrayable-interface](https://gitlab.com/php-extended/php-arrayable-interface)[![PHP](https://camo.githubusercontent.com/f6dbcddc03c4a81e710a5cbe69ee0732fc8059f23c392f39edf54b7243d759d3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7068702d657874656e6465642f7068702d617272617961626c652d696e746572666163652f706870)](http://php.net)Interface-only, strict contract definition[illuminate/contracts](https://github.com/illuminate/contracts)[![PHP](https://camo.githubusercontent.com/6f1a7a37f17d93bc852b6a093cb065354e4b538979f6c98ce0f57d27f8a92fec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f696c6c756d696e6174652f636f6e7472616374732f706870)](http://php.net)Defines a generic `Arrayable` interface usable with or without LaravelMissing an alternative? Submit a [pull request](https://github.com/imponeer/toarray-interface/compare) to get it added.

Development
-----------

[](#development)

To maintain code quality, this project uses:

- [PHP\_CodeSniffer (phpcs)](https://github.com/squizlabs/PHP_CodeSniffer) for coding standards.
- [PHPStan](https://phpstan.org/) for static analysis.

### Running PHPCS

[](#running-phpcs)

Run the following command to check code style:

```
vendor/bin/phpcs
```

### Running PHPStan

[](#running-phpstan)

Run the following command to perform static analysis:

```
vendor/bin/phpstan analyse
```

Refer to `phpcs.xml` and `phpstan.neon` for configuration details.

How to contribute?
------------------

[](#how-to-contribute)

Contributions are welcome! To contribute:

1. Fork this repository on GitHub.
2. Create a new branch for your feature or bugfix.
3. Make your changes and commit them with clear messages.
4. Push your branch to your fork and open a Pull Request.

If you find a bug or have a question, please use the [issues tab](https://github.com/imponeer/toarray-interface/issues).

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance70

Regular maintenance activity

Popularity26

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 65.2% 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 ~292 days

Recently: every ~244 days

Total

10

Last Release

394d ago

Major Versions

1.0.0 → 2.0.02018-09-14

PHP version history (3 changes)1.0.0PHP &gt;=5.0

2.0.0PHP &gt;=7.0

v2.1.0PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/dba53e399a507d1f60626164c8b1e340a5cc0dc4f0637f4c6aaff8fc239fdaee?d=identicon)[fiammybe](/maintainers/fiammybe)

![](https://avatars.githubusercontent.com/u/342641?v=4)[Raimondas Rimkevičius](/maintainers/MekDrop)[@MekDrop](https://github.com/MekDrop)

![](https://avatars.githubusercontent.com/u/2429107?v=4)[Steve Kenow](/maintainers/skenow)[@skenow](https://github.com/skenow)

---

Top Contributors

[![MekDrop](https://avatars.githubusercontent.com/u/342641?v=4)](https://github.com/MekDrop "MekDrop (58 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (23 commits)")[![fiammybe](https://avatars.githubusercontent.com/u/3736946?v=4)](https://github.com/fiammybe "fiammybe (4 commits)")[![Codex](https://avatars.githubusercontent.com/in/2248422?v=4)](https://github.com/Codex "Codex (2 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![mend-bolt-for-github[bot]](https://avatars.githubusercontent.com/in/16809?v=4)](https://github.com/mend-bolt-for-github[bot] "mend-bolt-for-github[bot] (1 commits)")

---

Tags

composer-libraryhacktoberfestinterfacephpphp-librarytoarraytoarray-interfaceinterfaceserializablearray conversioncontractobject conversionarrayabletoArrayphp-interface

###  Code Quality

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/imponeer-toarray-interface/health.svg)

```
[![Health](https://phpackages.com/badges/imponeer-toarray-interface/health.svg)](https://phpackages.com/packages/imponeer-toarray-interface)
```

###  Alternatives

[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89955.0M361](/packages/nette-robot-loader)[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31053.8M53](/packages/openlss-lib-array2xml)[getsolaris/laravel-make-service

A MVCS pattern create a service command for Laravel 5+

81173.8k](/packages/getsolaris-laravel-make-service)[fab2s/nodalflow

A PHP Nodal WorkFlow

16373.7k1](/packages/fab2s-nodalflow)

PHPackages © 2026

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