linchatter;10987288 wrote:
Think I was looking for something more generic
I don't see how it would get more generic than this. Actually, the inverse is more likely to be needed, where some special care might be needed for some property. That special care would go either into a setSomeAttribute() function, or into a special case in the __set() function.
linchatter;10987288 wrote:
which handled multi-level XML.
Do the job recursively and it doesn't matter how many nested objects you have, or put this conversion in the __set() or setSomePropertyWhichIsClassInstance().
The "only" thing you lack in an stdClass instance that you get in your particular class whatever it is, is the stuff which is generic to your class: statics properties, functions, constants and visibility modifiers (private, protected, public). What you have in the stdClass instance, without visibility modifiers, is whatever is particular to this class instance: the data. And that's why all you need to do is map this data into the properties of your class.
Assuming the property identifiers are identical, the process can be completely automated without handling of any special cases. For nested object, and assuming the property identifiers also match a classname, the entire process can still be completely automated without any special case handling.
It's only if some property identifier in stdClass doesn't match the same property identifier in yourClass, or if some property is an object while its identifier doesn't match the classname that this object should be transformed into that you need to handle special stuff. And that's easily handled by using an array or other similar structure that maps stdClass property identifiers into yourClass property identifiers and/or classnames.
So I still don't get what the point would be in spending time trying to find what you're looking for. As far as I understand your needs, I gave it to you, and it took 30 seconds to write the code for it, with another few minutes to write code for example data and the Person class, which is also just created as an example. You have your data (not coming from an array initially, but either way ending up as a standard object before the transform begins), and you have your end class(es).
Sure, dealing with the nested objects might take another minute to code, but it's along the lines of adding code for it in its specific setSomeObjectProperty() function, or in the __set() function: if ($propIdentifier == 'foo') { transform($propIdentifier, getClassNameForProp($propIdentifier) }, where transform() would be the function handling the transformation, whose code was not wrapped in a function in my little example.
Add in a third parameter, array $identifierMappings, and you're all set.