Skip to content

Commit 3ef3e56

Browse files
Rewrite count() page (#948)
* Rewrite count() page * Change methodname to interface name and fix example output * Add example with Countable object * Delete redundant code comments
1 parent c0954ae commit 3ef3e56

File tree

1 file changed

+54
-26
lines changed

1 file changed

+54
-26
lines changed

reference/array/functions/count.xml

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<refentry xml:id="function.count" xmlns="http://docbook.org/ns/docbook">
55
<refnamediv>
66
<refname>count</refname>
7-
<refpurpose>Count all elements in an array, or something in an object</refpurpose>
7+
<refpurpose>Counts all elements in an array or in a <interfacename>Countable</interfacename> object</refpurpose>
88
</refnamediv>
99

1010
<refsect1 role="description">
@@ -15,20 +15,9 @@
1515
<methodparam choice="opt"><type>int</type><parameter>mode</parameter><initializer><constant>COUNT_NORMAL</constant></initializer></methodparam>
1616
</methodsynopsis>
1717
<para>
18-
Counts all elements in an array, or something in an object.
19-
</para>
20-
<para>
21-
For objects, if you have
22-
<link linkend="ref.spl">SPL</link> installed, you can hook into
23-
<function>count</function> by implementing interface
24-
<classname>Countable</classname>. The interface has exactly one method,
25-
<methodname>Countable::count</methodname>, which returns the return value for the
26-
<function>count</function> function.
27-
</para>
28-
<para>
29-
Please see the <link linkend="language.types.array">Array</link>
30-
section of the manual for a detailed explanation of how arrays
31-
are implemented and used in PHP.
18+
Counts all elements in an array when used with an array. When used with an
19+
object that implements the <interfacename>Countable</interfacename> interface, it returns
20+
the return value of the method <methodname>Countable::count</methodname>.
3221
</para>
3322
</refsect1>
3423

@@ -40,7 +29,7 @@
4029
<term><parameter>value</parameter></term>
4130
<listitem>
4231
<para>
43-
An array or <classname>Countable</classname> object.
32+
An array or <interfacename>Countable</interfacename> object.
4433
</para>
4534
</listitem>
4635
</varlistentry>
@@ -71,11 +60,11 @@
7160
&reftitle.returnvalues;
7261
<para>
7362
Returns the number of elements in <parameter>value</parameter>.
74-
When the parameter is neither an array nor an object with
75-
implemented <classname>Countable</classname> interface,
76-
<literal>1</literal> will be returned.
77-
There is one exception, if <parameter>value</parameter> is &null;,
78-
<literal>0</literal> will be returned.
63+
Prior to PHP 8.0.0, if the parameter was neither an &array; nor an &object; that
64+
implements the <interfacename>Countable</interfacename> interface,
65+
<literal>1</literal> would be returned,
66+
unless <parameter>value</parameter> was &null;, in which case
67+
<literal>0</literal> would be returned.
7968
</para>
8069
</refsect1>
8170

@@ -112,7 +101,7 @@
112101
<refsect1 role="examples">
113102
&reftitle.examples;
114103
<para>
115-
<example>
104+
<example xml:id="count.example.basic">
116105
<title><function>count</function> example</title>
117106
<programlisting role="php">
118107
<![CDATA[
@@ -139,7 +128,7 @@ int(3)
139128
</example>
140129
</para>
141130
<para>
142-
<example>
131+
<example xml:id="count.example.badexample">
143132
<title><function>count</function> non Countable|array example (bad example - don't do this)</title>
144133
<programlisting role="php">
145134
<![CDATA[
@@ -186,7 +175,7 @@ Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Cou
186175
</example>
187176
</para>
188177
<para>
189-
<example>
178+
<example xml:id="count.example.recursive">
190179
<title>Recursive <function>count</function> example</title>
191180
<programlisting role="php">
192181
<![CDATA[
@@ -195,14 +184,52 @@ $food = array('fruits' => array('orange', 'banana', 'apple'),
195184
'veggie' => array('carrot', 'collard', 'pea'));
196185
197186
// recursive count
198-
echo count($food, COUNT_RECURSIVE); // output 8
187+
var_dump(count($food, COUNT_RECURSIVE));
199188
200189
// normal count
201-
echo count($food); // output 2
190+
var_dump(count($food));
202191
203192
?>
204193
]]>
205194
</programlisting>
195+
&example.outputs;
196+
<screen role="php">
197+
<![CDATA[
198+
int(8)
199+
int(2)
200+
]]>
201+
</screen>
202+
</example>
203+
</para>
204+
<para>
205+
<example xml:id="count.example.countable">
206+
<title><interfacename>Countable</interfacename> object</title>
207+
<programlisting role="php">
208+
<![CDATA[
209+
<?php
210+
class CountOfMethods implements Countable
211+
{
212+
private function someMethod()
213+
{
214+
}
215+
216+
public function count(): int
217+
{
218+
return count(get_class_methods($this));
219+
}
220+
}
221+
222+
$obj = new CountOfMethods();
223+
var_dump(count($obj));
224+
?>
225+
]]>
226+
</programlisting>
227+
&example.outputs;
228+
<screen role="php">
229+
<![CDATA[
230+
int(2)
231+
]]>
232+
</screen>
206233
</example>
207234
</para>
208235
</refsect1>
@@ -216,6 +243,7 @@ echo count($food); // output 2
216243
<member><function>empty</function></member>
217244
<member><function>strlen</function></member>
218245
<member><function>is_countable</function></member>
246+
<member><link linkend="language.types.array">Arrays</link></member>
219247
</simplelist>
220248
</para>
221249
</refsect1>

0 commit comments

Comments
 (0)