为什么`count(new stdClass);`返回1?

kol*_*pto 0 php count

stdClass在PHP应该是一个空的对象,但它count()1出于某种原因.为什么?

PHP> (array)(new stdClass);
// array(
// 
// )
PHP> empty(new stdClass);
// false
PHP> count(new stdClass);
// 1
Run Code Online (Sandbox Code Playgroud)

gpg*_*kko 8

如果您阅读了count函数文档,您将在本节中找到有关返回值的信息:

返回值

返回array_or_countable中的元素数.如果参数不是数组或不是具有已实现Countable接口的对象, 则将返回1.有一个例外,如果array_or_countable为NULL,则返回0.