Error with PHP 7: Cannot access property started with '\0'

Started by Romy_Magick, March 23, 2016, 17:53:16 PM

Previous topic - Next topic

Jumbo!

No, that won't work.

From PHP documentation:
QuoteIf 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;
}

Milbo

Quote from: Jumbo! on April 01, 2016, 13:13:41 PM

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;
}
}


Looks best, thank you
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/