VirtueMart Forum

VirtueMart 2 + 3 => Virtuemart Development and bug reports => Topic started by: Romy_Magick on March 23, 2016, 17:53:16 pm

Title: Error with PHP 7: Cannot access property started with '\0'
Post by: Romy_Magick on March 23, 2016, 17:53:16 pm
Hi there!

I tried PHP 7 with J 3.5 and VM 3.0.14, but VM doesn't operate fully functional then.
As I tested the payment process with Amazon payment plugin, the last site (.../mein-warenkorb/confirm) is a 404 with additional Info "Cannot access property started with '\0'".

The very same error showed up when I tried to set the not completed orders to "cancelled" in the order overview from VM. I had 6 orders which weren't processed completly. Of course I wanted to ser them cancelled in a bulk, but only one order status were changed after the error occured and I went back to the order overview. 

After switching back to PHP 5.6, no error occured. I don't know where to start to search. If you need any further information, please let me know.
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: amlil71 on March 24, 2016, 06:22:54 am
Hello, i try to use php 7 yesterday and i have the same errors with paiement process. I have the same error 404 page with the message "cannot access property start with /0" . Thank you. Regards.
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: ThiloS on March 24, 2016, 10:00:19 am
Same here!

Finished process, (with offline Payment) - then i can't reach the bills in the backend.
"Cannot access property started with '\0'".
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Jumbo! on March 24, 2016, 17:28:26 pm
Yes, it looks like a bug. Here is a quick solution to the problem.

Open administrator/components/com_virtuemart/models/orders.php

Find the following codes between lines 286 to 291:

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

Replace above by:

Code: [Select]
$pvar = get_object_vars($product);
foreach ( $pvar as $k => $v) {
if (!isset($item->$k) and '_' != substr($k, 0, 1)) {
$k = trim($k); // Just added this line.
$item->$k = $v;
}
}

Save the file.
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Romy_Magick on March 24, 2016, 19:11:02 pm
Jumbo's fix works for me. Thanks!
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: ThiloS on March 25, 2016, 09:12:57 am
jep. everything fine now with that extra line...

Thanks!
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: amlil71 on March 25, 2016, 09:55:54 am
Hello Jumbo,

thank you very much. It works very well now :)
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Milbo on March 27, 2016, 03:06:17 am
would be interesting to have a vmtrace there to see which object creates the problem
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: zbig on March 29, 2016, 18:53:59 pm
I had the same problem and your advices were yery helpful. I changed the file orders.php and it's going fine.
When I used the old file orders.php the error appeared. This is my log in the moment of error:

[Tue Mar 29 18:20:14 2016] [error] [client 00.000.0.00] PHP Warning:  Parameter 1 to plgSystemRsseo::handleError() expected to be a reference, value given in Unknown on line 0, referer: https://kredos.pl/sklep/cart
[Tue Mar 29 18:20:14 2016] [error] [client 00.000.0.00] PHP Fatal error:  Uncaught Error: Cannot access property started with '\\0' in /home/users/serwer/public_html/serwer_kredos/administrator/components/com_virtuemart/models/orders.php:289\nStack trace:\n#0 /home/users/serwer/public_html/serwer_kredos/administrator/components/com_virtuemart/models/orders.php(169): VirtueMartModelOrders->getOrder(247, false)\n#1 /home/users/serwer/public_html/serwer_kredos/components/com_virtuemart/helpers/cart.php(1167): VirtueMartModelOrders->getMyOrderDetails(247, '19250249', 'p_E7J4FTPP')\n#2
 
 This may be helpful.
Best regard. zbig
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: prokops on March 30, 2016, 13:12:48 pm
The code suggestion took care of my issue, the only remaining thing is this warning on the order page:

Quote
Warning
500

Is this something experienced by others ?


Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Jumbo! on March 30, 2016, 13:25:42 pm
the only remaining thing is this warning on the order page:
What exactly do you mean by order page? Can you be little more specific?
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: zbig on March 30, 2016, 14:07:21 pm
The code suggestion took care of my issue, the only remaining thing is this warning on the order page:

Quote
Warning
500

Is this something experienced by others ?

I had exactly the same error. He always appeared after ordering. After correcting the orders.php it is OK
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Milbo on March 31, 2016, 23:14:37 pm
I still wonder which property of the product starts with an empty char?
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Jumbo! on April 01, 2016, 10:00:50 am
It is not exactly an empty character but "*_". See the screenshot attached. You can't have a property starting with *.

$item->*_tbl becomes $item->\0_tbl
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Milbo on April 01, 2016, 12:24:57 pm
Ahh, I see, php7 adds a * for protected variables. Okey that explains it. I must admit, I did not work on the php7 compatibility, yet. Others work on it.

Does it work with if (!isset($item->$k) and '_' != substr($k, 0, 1) and '*_' != substr($k, 0, 1)) {
but without the trim?

I wonder if we should use a Constant to define if we search for _ or *, because the * sounds to be the future. The _ is just a convention, or?
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Jumbo! 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;
}
Title: Re: Error with PHP 7: Cannot access property started with '\0'
Post by: Milbo 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