mirror of
https://github.com/php/doc-de.git
synced 2026-04-29 01:43:20 +02:00
193 lines
4.8 KiB
XML
193 lines
4.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: e587d0655e426f97b3fcb431453da5030e743b23 Maintainer: simp Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
<sect1 xml:id="language.types.callable">
|
|
<title>Callbacks / Callables</title>
|
|
|
|
<para>
|
|
Callbacks können mit der Typdeklaration <type>callable</type>
|
|
gekennzeichnet werden.
|
|
</para>
|
|
|
|
<para>
|
|
Einige Funktionen wie <function>call_user_func</function> oder
|
|
<function>usort</function> akzeptieren eine benutzerdefinierte
|
|
Callback-Funktion als Argument. Callback-Funktionen können nicht nur
|
|
einfache Funktionen sein, sondern auch Methoden eines
|
|
<type>Object</type>s, inklusive statischer Klassenmethoden.
|
|
</para>
|
|
|
|
<sect2 xml:id="language.types.callable.passing">
|
|
<title>Übergabe</title>
|
|
|
|
<para>
|
|
Eine PHP-Funktion wird anhand ihres Namens als <type>String</type>
|
|
übergeben. Jede eingebaute oder benutzerdefinierte Funktion kann
|
|
verwendet werden, außer Sprachkonstrukten wie:
|
|
<function>array</function>, <function>echo</function>,
|
|
<function>empty</function>, <function>eval</function>,
|
|
<function>exit</function>, <function>isset</function>,
|
|
<function>list</function>, <function>print</function> oder
|
|
<function>unset</function>.
|
|
</para>
|
|
|
|
<para>
|
|
Eine Methode eines instantiierten <type>Object</type>s wird
|
|
übergeben als <type>Array</type> mit dem <type>Object</type>
|
|
an Index-Position 0 und dem Methodennamen an Position 1. Der Zugriff
|
|
auf als protected oder private markierte Methoden von innerhalb
|
|
der Klasse ist erlaubt.
|
|
</para>
|
|
|
|
<para>
|
|
Statische Klassenmethoden können auch übergeben werden, ohne dass ein
|
|
<type>Object</type> dieser Klasse instantiiert werden muss, indem
|
|
man entweder den Namen der Klasse statt eines <type>Object</type>s als
|
|
Index 0 übergibt oder indem man <literal>'ClassName::methodName'</literal>
|
|
übergibt.
|
|
</para>
|
|
|
|
<para>
|
|
Neben den üblichen benutzerdefinierten Funktionen können auch
|
|
<link linkend="functions.anonymous">anonyme Funktionen</link>
|
|
oder <link linkend="functions.arrow">Pfeilfunktionen</link>
|
|
als Callback-Argumente übergeben werden.
|
|
</para>
|
|
|
|
<note>
|
|
<para>
|
|
Seit PHP 8.1.0 können anonyme Funktionen auch mit der Syntax für
|
|
<link linkend="functions.first_class_callable_syntax">Callback-Funktionen als Objekte erster Klasse</link>
|
|
erstellt werden.
|
|
</para>
|
|
</note>
|
|
|
|
<para>
|
|
Generell kann jedes Objekt, welches
|
|
<link linkend="object.invoke">__invoke()</link> implementiert, als
|
|
Callback-Argument übergeben werden.
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Beispiele für Callback-Funktionen
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
// Eine Beispiel-Callback-Funktion
|
|
function my_callback_function() {
|
|
echo 'hello world!', PHP_EOL;
|
|
}
|
|
|
|
// Eine Beispiel-Callback-Methode
|
|
class MyClass {
|
|
static function myCallbackMethod() {
|
|
echo 'Hello World!', PHP_EOL;
|
|
}
|
|
}
|
|
|
|
// Typ 1: Einfaches Callback
|
|
call_user_func('my_callback_function');
|
|
|
|
// Typ 2: Statischer Methodenaufruf
|
|
call_user_func(array('MyClass', 'myCallbackMethod'));
|
|
|
|
// Typ 3: Aufruf einer Objektmethode
|
|
$obj = new MyClass();
|
|
call_user_func(array($obj, 'myCallbackMethod'));
|
|
|
|
// Typ 4: Statischer Methodenaufruf
|
|
call_user_func('MyClass::myCallbackMethod');
|
|
|
|
// Typ 5: Relativer statischer Methodenaufruf
|
|
class A {
|
|
public static function who() {
|
|
echo 'A', PHP_EOL;
|
|
}
|
|
}
|
|
|
|
class B extends A {
|
|
public static function who() {
|
|
echo 'B', PHP_EOL;
|
|
}
|
|
}
|
|
|
|
call_user_func(array('B', 'parent::who')); // A, seit PHP 8.2.0 veraltet
|
|
|
|
// Typ 6: Objekte die __invoke implementieren können als Callable verwendet werden
|
|
class C {
|
|
public function __invoke($name) {
|
|
echo 'Hello ', $name, PHP_EOL;
|
|
}
|
|
}
|
|
|
|
$c = new C();
|
|
call_user_func($c, 'PHP!');
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Callback-Beispiel mit einem Closure
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// Unser Closure
|
|
$double = function($a) {
|
|
return $a * 2;
|
|
};
|
|
|
|
// Dies ist unsere Menge an Zahlen
|
|
$numbers = range(1, 5);
|
|
|
|
// Hier verwenden wir das Callback, um
|
|
// den Wert jedes Elements in unserer
|
|
// Menge zu verdoppeln
|
|
$new_numbers = array_map($double, $numbers);
|
|
|
|
print implode(' ', $new_numbers);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
2 4 6 8 10
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
|
|
¬e.func-callback-exceptions;
|
|
</sect2>
|
|
|
|
</sect1>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|