PHP中的对象比较 [英] Objects comparison in PHP

查看:52
本文介绍了PHP中的对象比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO,

问题

这不是众所周知的,但是PHP允许比较对象-不仅在相等性 == 上-而且在< > 。但是-它是如何工作的?因此,如果我想创建可比较的对象-应该遵循哪些限制/规则?

It's not well-known, but PHP allows to compare objects - and not just on equality == - but on < and > too. But - how it works? So if I want to create comparable objects - what restrictions/rules they should follow?

最有用的情况是 DateTime() 对象-它们具有一定的时间戳,可以对其进行比较(这具有逻辑意义)。在 lxr 上,对 DateTime 。但是普通情况呢?

Most useful case is with DateTime() objects - they hold certain timestamp and they could be compared (and this has logical sense). On lxr there's some explanation for DateTime . But what about common case?

我有:

class C
{
   protected $holder;
   protected $mirror;
   public function __construct($h = null)
   {
      $this->holder=$h;
      $this->mirror=-1*$h;
   }
}


$one = new C(1);
$two = new C(2);
//false, false, true: used $holder
var_dump($one>$two, $one==$two, $one<$two);

-如果我要更改属性声明顺序,它将使用 $ mirror

-if I'll change properties declaration order, it will use $mirror:

class C
{
   //only order changed:
   protected $mirror;
   protected $holder;
   public function __construct($h = null)
   {
      $this->holder=$h;
      $this->mirror=-1*$h;
   }
}

$one = new C(1);
$two = new C(2);
//true, false, false: used $mirror
var_dump($one>$two, $one==$two, $one<$two);

因此,规则之一似乎是将使用第一个声明的属性。但是我为什么还不清楚为什么使用 protected 属性。

So it seems one of the 'rules' is that it will use first declared property. But why is it using protected property at all is not clear to me too.

现在,更复杂的示例:

Now, more complex sample:

class Test
{
  protected $a;
  protected $b;

  function __construct($a, $b)
  {
    $this->a = $a;
    $this->b = $b;
  }
}

$x = new Test(1, 2);
$y = new Test(1, 3);

// true, false, false
var_dump($x < $y, $x == $y, $x > $y);

$x = new Test(3, 1);
$y = new Test(2, 1);

// false, false, true
var_dump($x < $y, $x == $y, $x > $y);

-因此它将使用第一个不等于属性进行比较。但是上面的代码片段只是一些情况。我想确切地知道它是如何发生的以及为什么。因此,

-so it will use first not-equal property for comparison. But code snippets above are only some cases. I want to know exactly how it's happening and why. Thus,

问题

是:工作原理?我的意思是,更详细:

Is: how it works? I mean, more detailed:


  • 我可以依靠事实吗,PHP将使用第一个不相等的属性进行比较?

  • 如果属性数量不相等怎么办? (即在代码执行过程中将某些属性动态添加到实例中)

  • 我可以将受保护的 / private 属性是否总是要计入此类比较?

  • Can I rely on fact, that PHP will use first not-equal property for comparison?
  • What will be done if count of properties isn't equal? (i.e. some property was dynamically added to instance during code execution)
  • Can I treat protected/private properties as to be counted for such comparison always?

等-因此,如果还有其他一些会影响结果的条件/限制/规则-请发布。 文档状态仅针对 = = / === 比较。另外,比较不同类的实例也是不可能的,因为它将返回 false (显然)。

e.t.c. - so if there are some additional conditions/restrictions/rules that will affect result - please, post. Documentation states only for ==/=== comparison. Also, comparison of instances of different classes is out of the issue since it'll return false (obviously).

推荐答案

PHP按顺序(按声明顺序)比较对象属性,并在找到的第一个不相等属性处停止。没有记录这种行为,因此,很遗憾,除了查看PHP的来源之外,没有太多要说的。

PHP compares sequentially (in the order of declaration) the object properties and stops at the first inequal property found. This behavior is not documented, so there's not much to be said about it, sadly, other than looking at the source of PHP.

未记录的通常是不要依靠它。

Not documented is usually a synonym of "don't rely on it".

这篇关于PHP中的对象比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆