PHP如何使用SplObjectStorage count()函数?用法示例

2021年3月31日12:13:14 发表评论 47 次浏览

SplObjectStorage::count()function是PHP中的内置函数, 用于计算存储中的对象数

语法如下:

int SplObjectStorage::count()

参数:该函数不包含任何参数。

返回值:此函数返回存储中的对象数。

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

程序1:

<?php
  
// Declare Empty SplObjectStorage
$gfg = new SplObjectStorage();
$gfg1 = new StdClass;
$gfg2 = new StdClass;
$gfg3 = new StdClass;
  
// Add object to SplObjectStorage class
$gfg ->attach( $gfg1 );
$gfg ->attach( $gfg2 );
$gfg ->attach( $gfg3 );
  
// Use count() function to count object
var_dump( $gfg -> count ());
?>

输出如下:

int(3)

程式2:

<?php
  
// Declare Empty SplObjectStorage
$gfg = new SplObjectStorage();
$gfg1 = new StdClass;
$gfg2 = new StdClass;
$gfg3 = new StdClass;
  
// Add object to SplObjectStorage class
$gfg ->attach( $gfg1 );
$gfg ->attach( $gfg2 );
$gfg ->attach( $gfg3 );
  
// Use count in different way
// Passing object as parameter
var_dump( count ( $gfg ));
?>

输出如下:

int(3)

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


木子山

发表评论

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