http://sandbox.onlinephpfunctions.com/code/4d2cc3648aed58c0dad90c7868173a4775e5ba0c
IMHO a bug or need feature change
providing a object as a array index doesn't try to us __toString() method so some volatile object identifier is used to index the array, which is breaking any persistency. Type hinting solves that, but while other than "string" type hinting doesn't work on ob jects, the automatic conversion to string should be very intuitive.
PS: tried to submit bug, but withot patch the bugs are ignored, unfortunately, I don't C coding
<?php
class shop_product_id {
protected $shop_name;
protected $product_id;
function __construct($shop_name,$product_id){
$this->shop_name = $shop_name;
$this->product_id = $product_id;
}
function __toString(){
return $this->shop_name . ':' . $this->product_id;
}
}
$shop_name = 'Shop_A';
$product_id = 123;
$demo_id = $shop_name . ':' . $product_id;
$demo_name = 'Some product in shop A';
$all_products = [ $demo_id => $demo_name ];
$pid = new shop_product_id( $shop_name, $product_id );
echo "with type hinting: ";
echo ($demo_name === $all_products[(string)$pid]) ? "ok" : "fail";
echo "\n";
echo "without type hinting: ";
echo ($demo_name === $all_products[$pid]) ? "ok" : "fail";
echo "\n";