Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automapper for php?

Tags:

.net

php

Are there any .Net automapper equivalent libraries but for php?

i.e. something that will copy all info from one class to another, for example user <-> userDTO.

like image 521
jgauffin Avatar asked Mar 13 '11 14:03

jgauffin


People also ask

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

Is AutoMapper fast?

Automapper is slower when mapping a single object and collections but there is one exception. Automapper is considerably faster when mapping a List<T> of objects on . NET Core (It's still slower on full .

Why use AutoMapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

How does AutoMapper work internally?

How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.


1 Answers

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


Original answer: I haven't heard of it, but I assume this to be rather unlikely, since PHP does not support return types for methods or functions. How can you possibly map the properties of two different classes, if you can only reflect their names, but not their types?

You could still write a little static helper class that simply gets two flat objects and copies all values from one property to the other class' property with the same name using reflection, but this will only work, if you know what you do and the types are convertable (like scalar types string/int/bool).

Maybe you can put some more reliability into it by parsing the phpdoc part (if available) to get a clue about the return type.

like image 192
Jan Avatar answered Oct 09 '22 07:10

Jan