Automapper for php?

Are there any alternative autostart libraries, but for php?

i.e. that will copy all the information from one class to another, for example user ↔ userDTO .

+4
source share
1 answer

Update: Yes there is! I wrote a small rudimentary library, which can be downloaded here: http://automapper.nylle.de


Original answer: I have not heard about this, but I assume that this is unlikely, since PHP does not support return types for methods or functions. How can you map the properties of two different classes if you can only reflect their names, but not their types?

You can still write a small static helper class that simply gets two flat objects and copies all the values ​​from one property to the property of another class with the same name using reflection , but this will only work if you know what you are doing and types can be convertible (for example, scalar types string / INT / BOOL).

Perhaps you can add some robustness to parsing the phpdoc part (if any) to get an idea of ​​the return type.

+2
source

Source: https://habr.com/ru/post/1343531/


All Articles