3

在给出这个例子的RFC中:

// only values are compared
$a = (object) ["a" => "b"]; 
$b = (object) ["b" => "b"]; 
echo $a <=> $b; // 0

但是当我执行它时,我得到 1 作为输出:

$a = (object) ["a" => "b"];
$b = (object) ["b" => "b"];
echo $a <=> $b; //1

我现在不明白 <=> 如何比较对象?在 RFC 中,它说它仅按值进行比较。

PS我在vagrant下使用PHP 7.0.4-6+deb.sury.org~trusty+1 (cli) ( NTS )

升级版:

php > echo (object)["b"=>"b"] <=> (object)["b"=>"b"];
0
php > echo (object)["b"=>"b"] <=> (object)["a"=>"b"];
1
php > echo (object)["a"=>"b"] <=> (object)["b"=>"b"];
1
4

2 回答 2

4

在阅读RFC时,我们发现它自相矛盾:

添加一个新的运算符 (expr) <=> (expr),如果两个操作数相等,则返回 0,如果左侧更大,则返回 1,如果右侧更大,则返回 -1。它使用与我们现有的比较运算符完全相同的比较规则:<、<=、==、>= 和 >。(详见说明书)

注意:==,这意味着宇宙飞船操作员做了一个松散的比较。

稍后在示例中:

// only values are compared
$a = (object) ["a" => "b"]; 
$b = (object) ["b" => "b"]; 
echo $a  $b; // 0

spaceship 运算符只是运算符<==的组合>。它根据评估结果给出相应的返回值:

operator(s):   <  =  >
return value: -1  0  1

现在数组对象是有点复杂的类型。要了解 PHP spaceship 运算符的作用,<=>我们需要了解并了解如何处理数组和对象以及如何工作。<==>


因此,让我们看看每种类型的比较运算符<, >, ==。首先我们会看,<然后>我们也会看==

数组比较运算符

现在至于数组<,并>此处记录:

┌────────────┬────────────┬────────────────────────── ──────────────────────────┐
│ 类型 │ 类型 │ │
│ 操作数 1 │ 操作数 2 │ 结果 │
├────────────┼────────────┼────────────────────────── ──────────────────────────┤
│ 数组 │ 数组 │ 成员越少的数组越小, │
│ │ │ 如果在操作数 2 中找不到来自操作数 1 的键 │
│ │ │ 那么数组是不可比的, │
│ │ │ 否则 - 按值比较 │
└────────────┴────────────┴────────────────────────── ──────────────────────────┘

这也可以用代码编写和表示:

Example #2 标准数组比较的转录

<?php
// Arrays are compared like this with standard comparison operators
function standard_array_compare($op1, $op2)
{
    if (count($op1) < count($op2)) {
        return -1; // $op1 < $op2
    } elseif (count($op1) > count($op2)) {
        return 1; // $op1 > $op2
    }
    foreach ($op1 as $key => $val) {
        if (!array_key_exists($key, $op2)) {
            return null; // uncomparable
        } elseif ($val < $op2[$key]) {
            return -1;
        } elseif ($val > $op2[$key]) {
            return 1;
        }
    }
    return 0; // $op1 == $op2
}
?>

我们可以通过一些测试轻松地对此进行测试。使用数学之类的方法,总是只改变一件事,所以我们可以确保我们在这里是正确的:

/**
/*
/* Testing operators: < and >
/*
 */ 

//Test case
//Variations: amount, values and keys (order)
//Test count: 9
//    Failed: 0
//    Passed: 9    
{
    //Test case 1.1
    $a = [1];
    $b = [1];

    //Passed
    var_dump("Same amount of elements, keys and values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
   
    //Test case 1.2
    $a = [1];
    $b = [1, 1];

    //Passed
    var_dump("NOT same amount of elements, but same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));

    //Test case 1.3 
    $a = [10];
    $b = [1, 1];

    //Passed
    var_dump("NOT same amount of elements nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.4
    $a = [1 => 1];
    $b = [10 => 1];

    //Passed
    var_dump("Same amount of element and values, NOT same keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));

    //Test case 1.5
    $a = [10];
    $b = [1];

    //Passed
    var_dump("Same amount of elements and keys, NOT same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.6
    $a = [1 => 1, 2 => 1];
    $b = [2 => 1, 1 => 1];

    //Passed
    var_dump("Same amount of elements and keys in different order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.7
    $a = [1 => 1, 2 => 5];
    $b = [2 => 5];

    //Passed
    var_dump("Same values, NOT same amount of elements nor keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.8
    $a = [10 => 1];
    $b = [1 => 10];

    //Passed
    var_dump("NOT same keys nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.9
    $a = [1 => 1, 2 => 1];
    $b = [2 => 10, 1 => 1];

    //Passed
    var_dump("Same amount of elements and values, NOT same keys nor order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
}


echo PHP_EOL . PHP_EOL . PHP_EOL;  //Test case separator

/**
/*
/* Test case end
/*
 */

//NULL, TRUE, FALSE 2 str func
function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}

对于相等/恒等运算符==,我们在此处===找到数组的文档:

┌────────────┬──────────┬──────────────────────── ────────────────────────┐
│ 例子 │ 名称 │ 结果 │
├────────────┼──────────┼────────────────────────── ────────────────────────┤
│ $a == $b │ 平等 │ 如果 $a 和 $b 具有相同的键/值对,则为 TRUE。│
│ $a === $b │ 身份 │ 如果 $a 和 $b 具有相同的键/值对,则为 TRUE │
│ │ │ 顺序相同,类型相同。│
└────────────┴──────────┴────────────────────────── ────────────────────────┘

和以前一样,我们可以简单地使用一些测试代码进行测试:

/**
/*
/* Testing operators: == and ===
/*
 */ 

//Test case
//Variations: amount, values and keys (order)
//Test count: 5
//    Failed: 0
//    Passed: 5     
{
    //Test case 2.1
    $a = [1];
    $b = [1];

    //Passed
    var_dump("Same amount of elements, values and keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
   
    //Test case 2.2
    $a = [1];
    $b = [10, 1];

    //Passed
    var_dump("NOT same amount of elements, but same values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));

    //Test case 2.3
    $a = [10];
    $b = [1];

    //Passed
    var_dump("Same amount of elements, but not values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
    //Test case 2.4
    $a = [1 => 1];
    $b = [10 => 1];

    //Passed
    var_dump("Same amount of elements and values, but not keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
    //Test case 2.5
    $a = [1 => 1, 2 => 2];
    $b = [2 => 2, 1 => 1];

    //Passed
    var_dump("Same amount of elements, key and values, but different order: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
}


echo PHP_EOL . PHP_EOL . PHP_EOL;  //Test case separator

/**
/*
/* Test case end
/*
 */

//NULL, TRUE, FALSE 2 str func
function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}

因此,我们可以看到并确认数组的比较运算符按预期工作并按文档记录!

完整的测试文件

对象比较运算符

此处记录了对象<>对象的文档:

┌────────────┬────────────┬────────────────────────── ──────────────────────────┐
│ 类型 │ 类型 │ │
│ 操作数 1 │ 操作数 2 │ 结果 │
├────────────┼────────────┼────────────────────────── ──────────────────────────┤
│ 对象 │ 对象 │ 内置类可以定义自己的比较, │  
│ │ │ 不同的阶级是没有可比性的, │
│ │ │ 同类比较属性与数组相同 │
└────────────┴────────────┴────────────────────────── ──────────────────────────┘

和以前一样,我们也可以测试一下:

/**
/*
/* Testing operators: < and >
/*
 */ 

//Test case
//Variations: amount, values and keys (order)
//Test count: 10
//    Failed: 0
//    Passed: 10    
{
    //Test case 1.1
    $a = (object)["a" => 1];
    $b = (object)["a" => 1];

    //Passed
    var_dump("Same amount of elements, keys and values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
   
    //Test case 1.2
    $a = (object)["a" => 1];
    $b = (object)["a" => 1, "b" => 1];

    //Passed
    var_dump("NOT same amount of elements, but same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));

    //Test case 1.3 
    $a = (object)["a" => 10];
    $b = (object)["a" => 1, "b" => 1];

    //Passed
    var_dump("NOT same amount of elements nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.4
    $a = (object)["a" => 1];
    $b = (object)["b" => 1];

    //Passed
    var_dump("Same amount of element and values, NOT same keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));

    //Test case 1.5
    $a = (object)["a" => 10];
    $b = (object)["a" => 1];

    //Passed
    var_dump("Same amount of elements and keys, NOT same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.6
    $a = (object)["a" => 1, "b" => 1];
    $b = (object)["b" => 1, "a" => 1];

    //Passed
    var_dump("Same amount of elements and keys in different order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.7
    $a = (object)["a" => 1, "b" => 5];
    $b = (object)["b" => 5];

    //Passed
    var_dump("Same values, NOT same amount of elements nor keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.8
    $a = (object)["c" => 1];
    $b = (object)["a" => 10];

    //Passed
    var_dump("NOT same keys nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.9
    $a = (object)["a" => 1, "b" => 1];
    $b = (object)["b" => 10, "a" => 1];

    //Passed
    var_dump("Same amount of elements and values, NOT same keys nor order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
    //Test case 1.10
    class A {public $a = 1;}
    $a = new A;
    class B {public $a = 1;}
    $b = new B;

    //Passed
    var_dump("Same amount of elements and values and keys, but different  not built-in class: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
    
}


echo PHP_EOL . PHP_EOL . PHP_EOL;  //Test case separator

/**
/*
/* Test case end
/*
 */

//NULL, TRUE, FALSE 2 str func
function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}

==对象和对象的文档在此处===有自己的页面:

使用比较运算符(==)时,对象变量的比较方式很简单,即:如果两个对象实例具有相同的属性和值,并且是同一类的实例,则它们相等。

当使用恒等运算符 (===) 时,对象变量是相同的当且仅当它们引用同一类的同一实例。

再次可以测试:

/**
/*
/* Testing operators: == and ===
/*
 */ 

//Test case
//Variations: amount, values and keys (order)
//Test count: 7
//    Failed: 0
//    Passed: 7    
{
    //Test case 2.1
    $a = (object)["a" => 1];
    $b = (object)["a" => 1];

    //Passed
    var_dump("Same amount of elements, values and keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
   
    //Test case 2.2
    $a = (object)["a" => 1];
    $b = (object)["a" => 10, "b" => 1];

    //Passed
    var_dump("NOT same amount of elements, but same values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));

    //Test case 2.3
    $a = (object)["a" => 10];
    $b = (object)["a" => 1];

    //Passed
    var_dump("Same amount of elements, but not values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
    //Test case 2.4
    $a = (object)["a" => 1];
    $b = (object)["b" => 1];

    //Passed
    var_dump("Same amount of elements and values, but not keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
    //Test case 2.5
    $a = (object)["a" => 1, "b" => 2];
    $b = (object)["b" => 2, "a" => 1];

    //Passed
    var_dump("Same amount of elements, key and values, but different order: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
    //Test case 2.6
    class C {public $a = 1;}
    $a = new A;
    class D {public $a = 1;}
    $b = new B;

    //Passed
    var_dump("Same amount of elements and values and keys, but different  not built-in class: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
    //Test case 2.7
    $a = (object)["a" => 1];
    $b = $a;

    //Passed
    var_dump("Same exact instance: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
    
}


echo PHP_EOL . PHP_EOL . PHP_EOL;  //Test case separator

/**
/*
/* Test case end
/*
 */

//NULL, TRUE, FALSE 2 str func
function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}

所以我们看到,带有对象的比较运算符的行为与预期和记录的完全一样!即使进行松散的比较,也会考虑属性和值。

完整的测试文件


结论

由于此错误已在此处报告,因此错误报告可能基于 RFC 中的评论,该评论说:

// 只比较值

但除此之外,这是 RFC 中唯一带有注释的示例,RFC 明确声明它使用与<,==和相同的比较规则>

这意味着提供的代码示例将无法比较,因为它没有相同的属性/键。

至于相等性,它需要相同的属性/键和值,因此它不能相等,并且对于小于或大于它是不可比较的,如上面的代码示例所示,比较是如何工作的:

if (!array_key_exists($key, $op2)) {
    return null; // uncomparable
} 

如果我们单独尝试每个比较运算符,我们也会看到这一点:

$a = (object)["b" => "b"]; 
$b = (object)["a" => "b"]; 


var_dump($a > $b);   //FALSE
var_dump($a < $b);   //FALSE
var_dump($a == $b);  //FALSE

全部返回 false,因为它无法比较。

只是为了这个案例StdClass会有自己的比较,我们可以用我们自己的类来测试它:

class A {
public $a = "";
public $b = "";
}

$a = new A;
$a->a = "b";
unset($a->b);

$b = new A;
$b->b = "b";
unset($b->a);

var_dump($a);
var_dump($b);

var_dump($a <=> $b); //1

同样的输出:1。

所以我想说,既然它是无与伦比的,它不应该返回01也不应该-1。它可能应该返回FALSENULL类似的东西。

现在我会说这种行为没有正确记录。

于 2016-03-27T12:16:58.027 回答
0
echo $object1 <=> $object2; // 0

0仅在以下情况下才应返回

  • $object1并且$object2是同一类的实例
  • 它们具有相同的属性和值

在您的示例中,如果它返回0而不是1. 也许RFC中的错字?

于 2016-03-27T12:10:03.893 回答