您现在的位置是:首页 > PHP框架交流PHP框架交流
php把对象count 统计 实现Countable计算数量 php进阶
上善若水2022-01-02 16:04:55【PHP框架交流】
2088人已围观
简介php把对象count 统计 count计算数量,我们知道一般count作用于数量,用于计算数组里面的元素数量, class A implements \Countable { /**
php把对象count 统计 count计算数量,我们知道一般count作用于数量,用于计算数组里面的元素数量,
class A implements \Countable
{
/**
* @var array $data
*/
public $data = [];
public function count()
{
return count($this->data);
}
}
$a = new A();
$array = ['ak' => 'a'];
print_r(count($array));
如图就可以轻松实现对一个对象调用count() 函数了.打印结果为
/**
* Counts all elements in an array, or something in an object.
* <p>For objects, if you have SPL installed, you can hook into count() by implementing interface {@see Countable}.
* The interface has exactly one method, {@see Countable::count()}, which returns the return value for the count() function.
* Please see the {@see Array} section of the manual for a detailed explanation of how arrays are implemented and used in PHP.</p>
* @link https://php.net/manual/en/function.count.php
* @param array|Countable $value The array or the object.
* @param int $mode [optional] If the optional mode parameter is set to
* COUNT_RECURSIVE (or 1), count
* will recursively count the array. This is particularly useful for
* counting all the elements of a multidimensional array. count does not detect infinite recursion.
* @return int the number of elements in var, which is
* typically an array, since anything else will have one
* element.
* <p>
* If var is not an array or an object with
* implemented Countable interface,
* 1 will be returned.
* There is one exception, if var is null,
* 0 will be returned.
* </p>
* <p>
* Caution: count may return 0 for a variable that isn't set,
* but it may also return 0 for a variable that has been initialized with an
* empty array. Use isset to test if a variable is set.
* </p>
*/
#[Pure]
function count(Countable|array $value, int $mode = COUNT_NORMAL): int {}
源码内部就是Countable|array就可以调用这个方法.
很赞哦! (2)
相关文章
随机图文
开心一刻:笑死人不偿命的极品笑话
1、今天打雷,雷声有点点大,老公上班不在家。正想着是不是发条短信去装下可怜,结果老公电话来了,正高兴原来那货这么关心我,结果他说:把家里电插头拔了。我问他还有别的事没?他说没了。我说你咋不关心我啊,打雷呐!!!他哦了声说:那你小心点,别被雷劈了…被雷劈!!!我恨 2、今天去剪了个头发,后来理发师没要钱。 3、有个死党叫刘查向,,今天才知道她妈姓向,当我听到他妈姓向时,我笑喷了,呵呵,,他爸挺幽php技术提升心得与方法
现在的PHP市场虽然充斥了大量的的PHP开发人员,但这些人当中真正能称得上高手的却寥寥无几。很多公司虽然招聘了一些PHP开发人员,但是由于技术水平不高,导致公司的项目一直堆积。这不仅另公司无奈也让已经入职的PHP开发人员着急,他们也想要在PHP领域更近一步,但却苦于找不到提高自己的方法,下面我们的鸥仔收集了一些PHP大神的一些工作方式、习惯,让大家看看PHP大神们是如何工作,也希望这些方法能帮助到那些想要在PHP领域更近一步的人。深圳市九州电子有限公司php面试2022.04
深圳市九州电子有限公司公司是离职以来的首次面试,公司再离世界之窗两三站地铁,算是小公司,最近一直游玩,逃避面试,总之还得去,多面面找找感觉感觉吧.公司再6楼,办公地点不是很大.面试的话就一道类似屏风的phpstrom自动提示失效 给变量设置@var自动提示
我们在容器中取值$redis = \Yii::$app->redis;这个时候就没有提示,但是对我我们刚上手,想多了解里面的方法有那些的时候,设置自动提示的功能就十分有必要,采用注释的形式,不会对代码