| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
There have been several attempts to introduce a standard toArray method or interface in PHP:
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-interfaceAlternatively, 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:
Run the following command to check code style:
vendor/bin/phpcsRun the following command to perform static analysis:
vendor/bin/phpstan analyseRefer to phpcs.xml and phpstan.neon for configuration details.
Contributions are welcome! To contribute:
If you find a bug or have a question, please use the issues tab.
| Back | FazBrowse Home | New Git URL |