language/types/callable.xml
e587d0655e426f97b3fcb431453da5030e743b23
...
...
@@ -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>
...
...
@@ -38,18 +36,29 @@
38
36

39
37
<para>
40
38
Static class methods can also be passed without instantiating an
41
-
<type>object</type> of that class by passing the class name instead of an
42
-
<type>object</type> at index 0.
43
-
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
44
41
<literal>'ClassName::methodName'</literal>.
45
42
</para>
46
43

47
44
<para>
48
45
Apart from common user-defined function,
49
-
<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
50
48
passed to a callback parameter.
51
49
</para>
52
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
+

53
62
<para>
54
63
<example>
55
64
<title>
...
...
@@ -61,13 +70,13 @@
61
70

62
71
// An example callback function
63
72
function my_callback_function() {
64
-
echo 'hello world!';
73
+
echo 'hello world!', PHP_EOL;
65
74
}
66
75

67
76
// An example callback method
68
77
class MyClass {
69
78
static function myCallbackMethod() {
70
-
echo 'Hello World!';
79
+
echo 'Hello World!', PHP_EOL;
71
80
}
72
81
}
73
82

...
...
@@ -81,28 +90,28 @@ call_user_func(array('MyClass', 'myCallbackMethod'));
81
90
$obj = new MyClass();
82
91
call_user_func(array($obj, 'myCallbackMethod'));
83
92

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

87
-
// Type 5: Relative static class method call (As of PHP 5.3.0)
96
+
// Type 5: Relative static class method call
88
97
class A {
89
98
public static function who() {
90
-
echo "A\n";
99
+
echo 'A', PHP_EOL;
91
100
}
92
101
}
93
102

94
103
class B extends A {
95
104
public static function who() {
96
-
echo "B\n";
105
+
echo 'B', PHP_EOL;
97
106
}
98
107
}
99
108

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

102
-
// Type 6: Objects implementing __invoke can be used as callables (since PHP 5.3)
111
+
// Type 6: Objects implementing __invoke can be used as callables
103
112
class C {
104
113
public function __invoke($name) {
105
-
echo 'Hello ', $name, "\n";
114
+
echo 'Hello ', $name, PHP_EOL;
106
115
}
107
116
}
108
117

109
118