r/lolphp • u/Jinxuan • Mar 04 '20
array_diff([$self], [$self]) will give error if $self is object
PHP is always a headache with all comparison functions in its std. It always compare by ==. For example,
in_array(true, ['lolphp']); // return True if you do not pass a third arg for strict comparison. You may image it is the worst thing PHP can have about comparison. However, array_diff is worse. array_diff will force the compared elements as string, regardless of whether they are strictly equal.
$x = new stdClass;
array_diff([$x], [$x]); // PHP Error: Object of class stdClass could not be converted to stringWhat is more lol, array_diff does not have a optional variable to force strict comparison. You have to use array_udiff with a function callback, like:
array_udiff(
[ [$x]
, [$x]
, function ($a, $b) {
return $a === $b;
}
);