Author Topic: Error with PHP 7: Cannot access property started with '\0'  (Read 8991 times)

Jumbo!

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 669
  • Full-stack Web Developer
    • www.virtueplanet.com
  • VirtueMart Version: Always latest
Re: Error with PHP 7: Cannot access property started with '\0'
« Reply #15 on: April 01, 2016, 13:13:41 pm »
No, that won't work.

From PHP documentation:
Quote
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 -

Code: [Select]
$pvar = get_object_vars($product);
foreach ($pvar as $k => $v) {
if (!isset($item->$k) and isset($product->$k)) {
$item->$k = $v;
}
}

OR

Code: [Select]
$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.

Code: [Select]
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

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9853
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Error with PHP 7: Cannot access property started with '\0'
« Reply #16 on: April 01, 2016, 18:38:18 pm »

OR

Code: [Select]
$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
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/