I'm using a third party storage system that only returns me stdClass objects no matter what I feed in for some obscure reason. So I'm curious to know if there is a way to cast/convert an stdClass object into a full fledged object of a given type.
For instance something along the lines of:
//$stdClass is an stdClass instance
$converted = (BusinessClass) $stdClass;
I am just casting the stdClass into an array and feed it to the BusinessClass constructor, but maybe there is a way to restore the initial class that I am not aware of.
Note: I am not interested in 'Change your storage system' type of answers since it is not the point of interest. Please consider it more an academic question on the language capacities.
Cheers
Changed function for deep casting (using recursion)
Hope that somebody find this useful
consider adding a new method to BusinessClass:
then you can make a new BusinessClass from $stdClass:
BTW: Converting is highly important if you are serialized, mainly because the de-serialization breaks the type of objects and turns into stdclass, including DateTime objects.
I updated the example of @Jadrovski, now it allows objects and arrays.
example
example array
code: (its recursive). However, i don't know if its recursive with arrays. May be its missing an extra is_array