language/types/callable.xml
c897161ca5a62a887295c695adc161b8fde5d772
...
...
@@ -4,9 +4,7 @@
4
4
<title>Callbacks / Callables</title>
5
5

6
6
<para>
7
-
Callbacks can be denoted by <type>callable</type> type hint as of PHP 5.4.
8
-
This documentation used <type>callback</type> type information for the same
9
-
purpose.
7
+
Callbacks can be denoted by the <type>callable</type> type declaration.
10
8
</para>
11
9

12
10
<para>
...
...
@@ -32,23 +30,35 @@
32
30
<para>
33
31
A method of an instantiated <type>object</type> is passed as an
34
32
<type>array</type> containing an <type>object</type> at index 0 and the
35
-
method name at index 1.
33
+
method name at index 1. Accessing protected and private methods from
34
+
within a class is allowed.
36
35
</para>
37
36

38
37
<para>
39
38
Static class methods can also be passed without instantiating an
40
-
<type>object</type> of that class by passing the class name instead of an
41
-
<type>object</type> at index 0.
42
-
As of PHP 5.2.3, it is also possible to pass
39
+
<type>object</type> of that class by either, passing the class name
40
+
instead of an <type>object</type> at index 0, or passing
43
41
<literal>'ClassName::methodName'</literal>.
44
42
</para>
45
43

46
44
<para>
47
45
Apart from common user-defined function,
48
-
<link linkend="functions.anonymous">anonymous functions</link> can also be
46
+
<link linkend="functions.anonymous">anonymous functions</link> and
47
+
<link linkend="functions.arrow">arrow functions</link> can also be
49
48
passed to a callback parameter.
50
49
</para>
51
50

51
+
<note>
52
+
<para>
53
+
As of PHP 8.1.0, anonymous functions can also be created using the <link linkend="functions.first_class_callable_syntax">first class callable syntax</link>.
54
+
</para>
55
+
</note>
56
+

57
+
<para>
58
+
Generally, any object implementing <link linkend="object.invoke">__invoke()</link> can also
59
+
be passed to a callback parameter.
60
+
</para>
61
+

52
62
<para>
53
63
<example>
54
64
<title>
...
...
@@ -80,10 +90,10 @@ call_user_func(array('MyClass', 'myCallbackMethod'));
80
90
$obj = new MyClass();
81
91
call_user_func(array($obj, 'myCallbackMethod'));
82
92

83
-
// Type 4: Static class method call (As of PHP 5.2.3)
93
+
// Type 4: Static class method call
84
94
call_user_func('MyClass::myCallbackMethod');
85
95

86
-
// Type 5: Relative static class method call (As of PHP 5.3.0)
96
+
// Type 5: Relative static class method call
87
97
class A {
88
98
public static function who() {
89
99
echo "A\n";
...
...
@@ -96,7 +106,17 @@ class B extends A {
96
106
}
97
107
}
98
108

99
-
call_user_func(array('B', 'parent::who')); // A
109
+
call_user_func(array('B', 'parent::who')); // A, deprecated as of PHP 8.2.0
110
+

111
+
// Type 6: Objects implementing __invoke can be used as callables
112
+
class C {
113
+
public function __invoke($name) {
114
+
echo 'Hello ', $name, "\n";
115
+
}
116
+
}
117
+

118
+
$c = new C();
119
+
call_user_func($c, 'PHP!');
100
120
?>
101
121
]]>
102
122
</programlisting>
...
...
@@ -136,15 +156,6 @@ print implode(' ', $new_numbers);
136
156
</example>
137
157
</para>
138
158

139
-
<note>
140
-
<simpara>
141
-
In PHP 4, it was necessary to use a reference to create a callback that
142
-
points to the actual <type>object</type>, and not a copy of it. For more
143
-
details, see
144
-
<link linkend="language.references">References Explained</link>.
145
-
</simpara>
146
-
</note>
147
-

148
159
&note.func-callback-exceptions;
149
160
</sect2>
150
161

151
162