No, that won't work.
From PHP documentation:If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.
If you do not want to use trim then you can use this -
$pvar = get_object_vars($product);
foreach ($pvar as $k => $v) {
if (!isset($item->$k) and isset($product->$k)) {
$item->$k = $v;
}
}
OR
$pvar = get_object_vars($product);
foreach ($pvar as $k => $v) {
if (!isset($item->$k) and isset($product->$k) and '_' != substr($k, 0, 1)) {
$item->$k = $v;
}
}
Finally if you want to convert the object itself correctly to an array then you can use the following function.
function object_to_array($object)
{
$reflectionClass = new ReflectionClass(get_class($object));
$array = array();
foreach ($reflectionClass->getProperties() as $property)
{
$property->setAccessible(true);
$array[$property->getName()] = $property->getValue($object);
$property->setAccessible(false);
}
return $array;
}