php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75839 Undefined relation when objects compared
Submitted: 2018-01-18 03:25 UTC Modified: 2018-01-18 05:54 UTC
From: csongor at halmai dot hu Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2018-01-18 03:25 UTC] csongor at halmai dot hu
Description:
------------
When I compare two obejcts from two different but identially-looking classes then the comparison operators yield contradicting results.

In the samle code below, there are two variables $a and $b which are not in ==, nor in === nor in <= nor in >= relationship.

Moreover, the spaceship operator tells that $a>$b and $b>$a as well. 

Test script:
---------------
	error_reporting(E_ALL);
	
	class Objy_1 {
		public $v =1;
	}
	
	class Objy_2 {
		public $v =2;
	}

	$a = new Objy_1();
	$b = new Objy_2();
	
	var_dump($a < $b);    // false
	var_dump($a > $b);    // false
	var_dump($a == $b);   // false
	var_dump($a === $b);  // false
	var_dump($a <=> $b);  // false
	var_dump($a <=> $b);  // 1 which means $a > $b
	var_dump($b <=> $a);  // 1 which means $b > $a


Expected result:
----------------
I would expect non-contradicting results. In this concrete cases, I think $a == $b should be true, $a <=> $b and $b <=> $a should be 0.

Moreover, I think it would be nice to get a warning if I want to compare objects from different classes.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-18 05:54 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-01-18 05:54 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Objects of different classes are not comparable.
http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 22:01:28 2024 UTC