Skip to content

Commit 65a8086

Browse files
committed
Update readme
1 parent 5d7ebb6 commit 65a8086

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313

1414
## Motivation
1515

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

17-
Unfortunately PHP does not have a nice way how to typecast objects to `array`, like:
18+
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.
19+
20+
But so far (at least) there is no way to implement some (not even magic) method to be called when cast to `array`.
21+
22+
Ideally, something like this would work:
1823

1924
```php
2025
class Person
@@ -47,7 +52,7 @@ var_dump($personArray);
4752
*/
4853
```
4954

50-
But actually it cast to array like this:
55+
but actually it cast to array like this:
5156

5257
```php
5358
/*
@@ -60,9 +65,6 @@ var_dump($personArray);
6065
*/
6166
```
6267

63-
There is the [`__toString`](https://www.php.net/manual/en/class.stringable.php) magic method (since PHP 8.0) and the [`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.
64-
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.
65-
6668

6769
## Usage example
6870

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"description": "PHP arrayable interface for handling casting to an array",
44
"keywords": [
55
"inspirum",
6-
"arrayable"
6+
"arrayable",
7+
"object-to-array"
78
],
89
"homepage": "https://github.com/inspirum/arrayable-php",
910
"license": "MIT",
@@ -38,7 +39,7 @@
3839
},
3940
"autoload-dev": {
4041
"psr-4": {
41-
"Inspirum\\XML\\Tests\\": "tests"
42+
"Inspirum\\Arrayable\\Tests\\": "tests"
4243
}
4344
},
4445
"minimum-stability": "dev",

0 commit comments

Comments
 (0)