PHP SPL Countable接口示例如何应用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计304个文字,预计阅读时间需要2分钟。
本示例介绍了PHP标准库(SPL)中的Countable接口用法。Countable接口允许类使用count()函数。
phpclass MyClass implements Countable { protected $elements=[];
public function addElement($element) { $this->elements[]=$element; }
public function count(): int { return count($this->elements); }}
// 使用$myClass=new MyClass();$myClass->addElement('Apple');$myClass->addElement('Banana');$myClass->addElement('Cherry');
echo count($myClass); // 输出: 3
本文实例讲述了PHP标准库 (SPL)之Countable用法。分享给大家供大家参考,具体如下:
类实现 Countable 可被用于 count() 函数.
接口摘要
Countable { /* 方法 */ abstract public count ( void ) : int }
当一个类实现了Countable接口,实现了接口中的count方法,即可直接使用count(Object)的到count方法返回的值。
本文共计304个文字,预计阅读时间需要2分钟。
本示例介绍了PHP标准库(SPL)中的Countable接口用法。Countable接口允许类使用count()函数。
phpclass MyClass implements Countable { protected $elements=[];
public function addElement($element) { $this->elements[]=$element; }
public function count(): int { return count($this->elements); }}
// 使用$myClass=new MyClass();$myClass->addElement('Apple');$myClass->addElement('Banana');$myClass->addElement('Cherry');
echo count($myClass); // 输出: 3
本文实例讲述了PHP标准库 (SPL)之Countable用法。分享给大家供大家参考,具体如下:
类实现 Countable 可被用于 count() 函数.
接口摘要
Countable { /* 方法 */ abstract public count ( void ) : int }
当一个类实现了Countable接口,实现了接口中的count方法,即可直接使用count(Object)的到count方法返回的值。

