DEV Community

macnux
macnux

Posted on

4 1

Convert An Array To Object Using PHP (Hydrator Pattern)

In this post, we will convert an array to object using PHP hydrator pattern. This method is so simple, it’s about transferring data from one place to another.

We will define a class that will take an array and an object as inputs and search for all set() methods in the object and fills it with values from the array.

https://likegeeks.com/convert-array-to-object-using-php/

Thanks in advance.

Top comments (1)

Collapse
 
un3x profile image
Thomas Comes • Edited

Thanks for your post.
You should warn that using reflexion has performances issues.
Plus you have a strong binding between the structure of your array and the structure of your class.
Moreover you should use the hydrator pattern naming convention(as you mentionned it in your title).
Nontheless that's a cool code :)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay