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()
. 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. 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) 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 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.
To install and use this package, use Composer:
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.
use Imponeer\ToArrayInterface;
class DummyObject implements ToArrayInterface {
/**
* Converts object to array
*
* @return array<string, mixed>
*/
public function toArray(): array {
return [
'hash' => sha1(time()),
];
}
}
$instance = new DummyObject();
if ($instance instanceof ToArrayInterface) {
var_dump($instance->toArray());
}
This repository features an automatically updated GitHub 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.
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:
Package | PHP Version | Features |
---|---|---|
inspirum/arrayable | Recursive conversion, helper functions, base classes | |
dmytrof/array-convertible | Lightweight, nested conversion, simple interface | |
rexlabsio/array-object-php | Fluent interface, toArray() and toJson() methods |
|
php-extended/php-arrayable-interface | Interface-only, strict contract definition | |
illuminate/contracts | Defines a generic Arrayable interface usable with or without Laravel |
Missing an alternative? Submit a pull request to get it added.
To maintain code quality, this project uses:
- PHP_CodeSniffer (phpcs) for coding standards.
- PHPStan for static analysis.
Run the following command to check code style:
vendor/bin/phpcs
Run the following command to perform static analysis:
vendor/bin/phpstan analyse
Refer to phpcs.xml
and phpstan.neon
for configuration details.
Contributions are welcome! To contribute:
- Fork this repository on GitHub.
- Create a new branch for your feature or bugfix.
- Make your changes and commit them with clear messages.
- 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.