问题 SplObjectStorage :: contains和SplObjectStorage :: offsetExists有什么区别?


PHP文档不是很明确,只说明:

SplObjectStorage :: offsetExists  检查存储中是否存在对象。 (PHP> = 5.3.0)

SplObjectStorage ::包含  检查存储是否包含提供的对象。 (PHP> = 5.1.0)

这对我来说几乎是一回事。

题: 除了offsetExists仅在5.3.0中可用,2之间有什么区别?


我进行的小测试......

$s = new SplObjectStorage();
$o1 = new StdClass();
$o2 = new StdClass();
$o3 = "I'm not an object!";
$s->attach($o1);

var_dump($s->contains($o1));
var_dump($s->offsetExists($o1));
echo '<br>';
var_dump($s->contains($o2));
var_dump($s->offsetExists($o2));
echo '<br>';
var_dump($s->contains($o3));
var_dump($s->offsetExists($o3));

输出:

boolean true
boolean true

boolean false
boolean false

Warning: SplObjectStorage::contains() expects parameter 1 to be object, string given in index.php on line 15
null

Warning: SplObjectStorage::offsetExists() expects parameter 1 to be object, string given in index.php on line 16
null

2212
2017-08-16 08:08


起源



答案:


它们都完全一样。

offsetExists 定义为a 方法别名 的 contains 并且仅仅是为了遵守 ArrayAccess 接口。

您可以 在源头看自己 那 SPL_MA (方法别名)正在使用,并且还设置了几个其他别名。

  • offsetExists =包含
  • offsetSet = attach
  • offsetUnset = detach

12
2017-08-16 08:23



感谢您潜入源头。 - PeeHaa
太好了,非常感谢! - Tivie


答案:


它们都完全一样。

offsetExists 定义为a 方法别名 的 contains 并且仅仅是为了遵守 ArrayAccess 接口。

您可以 在源头看自己 那 SPL_MA (方法别名)正在使用,并且还设置了几个其他别名。

  • offsetExists =包含
  • offsetSet = attach
  • offsetUnset = detach

12
2017-08-16 08:23



感谢您潜入源头。 - PeeHaa
太好了,非常感谢! - Tivie