PHP SplObjectStorage contains()函数用法详细介绍

2021年3月17日14:35:26 发表评论 608 次浏览

SplObjectStorage :: contains()函数是PHP中的内置函数, 用于检查存储对象是否包含指定的对象。

语法如下:

bool SplObjectStorage::contains( $value )

参数:该函数接受单个参数$值它指定要检查的存储对象。

返回值:如果存储对象包含指定的对象, 则此函数返回true, 否则返回false。

下面的程序说明了SplObjectStorage :: contains()PHP中的功能:

程序1:

<?php
$gfg1 = new StdClass;
$gfg2 = new StdClass;
  
// Declare Empty SplObjectStorage
$str = new SplObjectStorage();
  
$str [ $gfg1 ] = "lsbin" ;
  
// Print result
var_dump( $str ->contains( $gfg1 ));
var_dump( $str ->contains( $gfg2 ));
  
?>

输出如下:

bool(true)
bool(false)

程式2:

<?php
$gfg1 = new StdClass;
$gfg2 = new StdClass;
  
// Declare Empty SplObjectStorage
$str = new SplObjectStorage();
  
$str [ $gfg1 ] = "lsbin" ;
  
// Print result
var_dump( $str ->contains( $gfg1 ));
var_dump( $str ->contains( $gfg2 ));
  
// detach and print result
$str ->detach( $gfg1 );
var_dump( $str ->contains( $gfg1 ));
  
?>

输出如下:

bool(true)
bool(false)
bool(false)

参考: https://www.php.net/manual/en/splobjectstorage.contains.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: