<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'language.types.declarations.php',
    1 => 'Typdeklarationen',
    2 => 'Typdeklarationen',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Typen',
  ),
  'prev' => 
  array (
    0 => 'language.types.iterable.php',
    1 => 'Iterable',
  ),
  'next' => 
  array (
    0 => 'language.types.type-juggling.php',
    1 => 'Typumwandlung (Typen-Jonglage)',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'language/types/declarations.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.declarations" class="sect1">
 <h2 class="title">Typdeklarationen</h2>

 <p class="para">
  Typen können für Funktionsargumente, Rückgabewerte, seit PHP 7.4.0 für
  Klasseneigenschaften und seit PHP 8.3.0 für Klassenkonstanten deklariert
  werden. Dadurch wird sichergestellt, dass der Wert zum Zeitpunkt des Aufrufs
  vom angegebenen Typ ist und andernfalls ein <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span>
  geworfen.
 </p>

 <p class="para">
  Mit Ausnahme des Typs <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> können alle von PHP
  unterstützten Typen in einer benutzerdefinierten Typdeklaration verwendet
  werden. Diese Seite enthält eine Changelog über die Verfügbarkeit der
  verschiedenen Typen und eine Dokumentation über ihre Verwendung in
  Typdeklarationen.
 </p>

 <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
  <p class="para">
   Wenn eine Klasse eine Methode einer Schnittstelle implementiert oder eine
   Methode neu implementiert, die bereits von der Elternklasse definiert
   wurde, muss sie mit der obigen Definition kompatibel sein. Eine Methode ist
   kompatibel, wenn sie den Regeln der
   <a href="language.oop5.variance.php" class="link">Kovarianz und Kontravarianz</a>
   folgt.
  </p>
 </p></blockquote>

 <div class="sect2">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Beschreibung</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       Unterstützung für die Typisierung von Klassen-, Interface-, Trait- und
       Enum-Konstanten wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Unterstützung für <abbr title="Disjunctive Normal Form">DNF</abbr>-Typen wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Unterstützung für den literalen Typ <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span> wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Die Typen <span class="type"><a href="language.types.null.php" class="type null">null</a></span> und <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> können nun
       eigenständig verwendet werden.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Unterstützung für Schnittmengentypen wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Die Rückgabe per Referenz aus einer <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span>-Funktion ist nun
       veraltet.
      </td>
     </tr>

     <tr>
      <td>8.1.0</td>
      <td>
       Unterstützung für den Rückgabetyp <span class="type"><a href="language.types.never.php" class="type never">never</a></span> wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Unterstützung für <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Unterstützung für den reinen Rückgabetyp <span class="type">static</span> wurde
       hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Unterstützung für Union-Typen wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       Unterstützung für die Typisierung von Klasseneigenschaften wurde
       hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       Unterstützung für <span class="type"><a href="language.types.object.php" class="type object">object</a></span> wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Unterstützung für <span class="type"><a href="language.types.iterable.php" class="type iterable">iterable</a></span> wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Unterstützung für <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span> wurde hinzugefügt.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       Unterstützung für nullable Typen wurde hinzugefügt.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.types.declarations.base">
  <h3 class="title">Hinweise zur Verwendung von atomaren (einfachen) Typen</h3>

  <p class="simpara">
   Atomare Typen verhalten sich einfach, wobei es einige kleinere
   Einschränkungen gibt, auf die in diesem Abschnitt eingegangen wird.
  </p>

  <div class="sect3" id="language.types.declarations.base.scalar">
   <h4 class="title">Skalare Typen</h4>
   <div class="warning"><strong class="warning">Warnung</strong>
    <p class="para">
     Namens-Aliase für skalare Typen (<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>, <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>,
     <span class="type"><a href="language.types.float.php" class="type float">float</a></span>, <span class="type"><a href="language.types.string.php" class="type string">string</a></span>) werden nicht unterstützt.
     Stattdessen werden sie wie die Namen von Klassen oder Schnittstellen
     behandelt. Wird zum Beispiel <code class="literal">boolean</code> als Typdeklaration
     verwendet, muss der Wert ein <a href="language.operators.type.php" class="link"><code class="literal">instanceof</code></a> der Klasse oder Schnittstelle
     <code class="literal">boolean</code> sein, und nicht vom Typ <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>:
    </p>
    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />    </span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">boolean $param</span><span style="color: #007700">) {}<br />    </span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     <p class="para">Das oben gezeigte Beispiel erzeugt mit PHP 8 folgende Ausgabe:</p>
     <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Warning: &quot;boolean&quot; will be interpreted as a class name. Did you mean &quot;bool&quot;? Write &quot;\boolean&quot; to suppress this warning in /in/9YrUX on line 2

Fatal error: Uncaught TypeError: test(): Argument #1 ($param) must be of type boolean, bool given, called in - on line 3 and defined in -:2
Stack trace:
#0 -(3): test(true)
#1 {main}
  thrown in - on line 2
</pre></div>
     </div>
    </div>
   </div>
  </div>

  <div class="sect3" id="language.types.declarations.void">
   <h4 class="title">void</h4>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Die Rückgabe per Referenz aus einer Funktion, die <span class="type"><span class="type"><a href="language.types.void.php" class="type void">void</a></span></span>
     zurückgibt, ist seit PHP 8.1.0 veraltet, da eine solche Funktion
     widersprüchlich ist. Vor PHP 8.1.0 gab sie bereits folgende
     <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> aus, wenn sie aufgerufen wurde:
     <span class="computeroutput">Only variable references should be returned by reference</span>.
     <div class="informalexample">
      <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function &amp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(): </span><span style="color: #0000BB">void </span><span style="color: #007700">{}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
   </p></blockquote>
  </div>

  <div class="sect3" id="language.types.declarations.base.function">
   <h4 class="title">Callable</h4>
   <p class="para">
    Dieser Typ kann nicht als Typdeklaration für die Eigenschaft einer Klasse
    verwendet werden.
   </p>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Es ist nicht möglich, eine Signatur für die Funktion anzugeben.
    </span>
   </p></blockquote>
  </div>

  <div class="sect3" id="language.types.declarations.references">
   <h4 class="title">Typdeklarationen für Parameter, die per Referenz übergeben werden</h4>

   <p class="simpara">
    Wenn ein per Referenz übergebener Parameter eine Typdeklaration hat, wird
    der Typ der Variablen <em>nur</em> beim Eintritt in die
    Funktion, zu Beginn des Aufrufs, überprüft, nicht aber bei der Rückgabe
    der Funktion. Das bedeutet, dass eine Funktion den Typ einer
    Variablenreferenz ändern kann.
   </p>
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Typisierte Parameter, die per Referenz übergeben werden</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(array &amp;</span><span style="color: #0000BB">$param</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$param </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$var </span><span style="color: #007700">= [];<br /></span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt
eine ähnliche Ausgabe wie:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
int(1)

Fatal error: Uncaught TypeError: array_baz(): Argument #1 ($param) must be of type array, int given, called in - on line 9 and defined in -:2
Stack trace:
#0 -(9): array_baz(1)
#1 {main}
  thrown in - on line 2
</pre></div>
    </div>
   </div>
  </div>
 </div>

 <div class="sect2" id="language.types.declarations.composite">
  <h3 class="title">Hinweise zur Verwendung von zusammengesetzten Typen</h3>
  <p class="para">
   Bei der Deklaration von zusammengesetzten Typen gibt es mehrere
   Einschränkungen und um einfache Fehler zu vermeiden, wird bei der
   Kompilierung eine Redundanzprüfung durchgeführt.
  </p>

  <div class="caution"><strong class="caution">Achtung</strong>
   <p class="simpara">
    Vor PHP 8.2.0, d. h. vor der Einführung von <abbr title="Disjunctive Normal Form">DNF</abbr>-Typen,
    war es nicht möglich, Schnittmengentypen (Intersection-Typen) mit
    zusammengefassten Typen (Union-Typen) zu kombinieren.
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.composite.union">
   <h4 class="title">Zusammengefasste Typen (Union-Typen)</h4>
   <div class="warning"><strong class="warning">Warnung</strong>
    <p class="simpara">
     Es ist nicht möglich, die beiden Singleton-Typen <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> und
     <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span> in einem Union-Typ zu kombinieren; stattdessen kann
     <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> verwendet werden.
    </p>
   </div>

   <div class="caution"><strong class="caution">Achtung</strong>
    <p class="simpara">
     Da <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> und <span class="type"><a href="language.types.null.php" class="type null">null</a></span> vor PHP 8.2.0 nicht als
     eigenständige Typen verwendet werden konnten, war ein Union-Typ, der nur
     aus diesen Typen bestand, nicht erlaubt. Hierbei handelt es sich um die
     folgenden Typen: <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>, <code class="literal">false|null</code> und
     <code class="literal">?false</code>.
    </p>
   </div>

   <div class="sect4" id="language.types.declarations.nullable">
    <h5 class="title">Syntaktischer Zucker für den Typ nullable</h5>

    <p class="para">
     Wenn ein einzelner Basistyp deklariert wird, kann dieser als nullable
     gekennzeichnet werden, indem dem Typnamen ein Fragezeichen
     (<code class="literal">?</code>) vorangestellt wird. Somit bedeuten
     <code class="literal">?T</code> und <code class="literal">T|null</code> dasselbe.
    </p>

    <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
     <span class="simpara">
      Diese Syntax wird seit PHP 7.1.0 unterstützt und geht der Unterstützung
      von allgemeinen Union-Typen voraus.
     </span>
    </p></blockquote>

    <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
     <p class="para">
      Es ist auch möglich, nullable Parameter anzugeben, indem
      <code class="literal">null</code> zum Standardwert gemacht wird. Dies ist jedoch
      nicht empfehlenswert, denn wenn der Standardwert in einer Kindklasse
      geändert wird, wird eine Verletzung der Typkompatibilität ausgelöst, da
      der Typ <span class="type"><a href="language.types.null.php" class="type null">null</a></span> zur Typdeklaration hinzugefügt werden muss.
      Darüber hinaus ist dieses Verhalten seit PHP 8.4 veraltet.
     </p>
     <div class="example" id="example-2">
      <p><strong>Beispiel #2 Die alte Methode, um Parameter nullable zu machen</strong></p>
      <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">C $c </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

      <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
      <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
      </div>
     </div>
    </p></blockquote>
   </div>
  </div>

  <div class="sect3" id="language.types.declarations.composite.redundant">
   <h4 class="title">Doppelte und redundante Typen</h4>
   <p class="para">
    Um einfache Fehler in Deklarationen von zusammengesetzten Typen
    abzufangen, führen redundante Typen, die ohne Laden einer Klasse erkannt
    werden können, zu einem Fehler beim Kompilieren. Dies schließt Folgendes
    ein:

    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       Jeder Typ, dessen Name aufgelöst wird, darf nur einmal vorkommen. Typen
       wie <code class="literal">int|string|INT</code> oder
       <code class="literal">Countable&amp;Traversable&amp;COUNTABLE</code> führen zu
       einem Fehler.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Die Verwendung des Typs <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> oder <span class="type"><a href="language.types.never.php" class="type never">never</a></span> führt
       zu einem Fehler.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">Bei zusammengesetzen Typen:</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         Wenn der Typ <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> verwendet wird, können nicht
         zusätzlich die Typen <span class="type"><a href="language.types.singleton.php" class="type false">false</a></span> oder <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span>
         verwendet werden.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Wenn der Typ <span class="type"><a href="language.types.object.php" class="type object">object</a></span> verwendet wird, können nicht
         zusätzlich Klassentypen verwendet werden.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Wenn der Typ <span class="type"><a href="language.types.iterable.php" class="type iterable">iterable</a></span> verwendet wird, können nicht
         zusätzlich <span class="type"><a href="language.types.array.php" class="type array">array</a></span> und <span class="classname"><a href="class.traversable.php" class="classname">Traversable</a></span>
         verwendet werden.
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">Bei Schnittmengentypen:</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         Die Verwendung eines Typs, der kein Klassentyp ist, führt zu einem
         Fehler.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Die Verwendung von <span class="type">self</span>, <span class="type">parent</span> oder
         <span class="type">static</span> führt zu einem Fehler.
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">Bei <abbr title="Disjunctive Normal Form">DNF</abbr>-Typen:</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         Wenn ein allgemeinerer Typ verwendet wird, ist der restriktivere Typ
         überflüssig.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         Die Verwendung zweier identischer Schnittmengentypen.
        </span>
       </li>
      </ul>
     </li>
    </ul>
   </p>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Dies garantiert nicht, dass der Typ &quot;minimal&quot; ist, weil dies bedeuten
     würde, dass alle verwendeten Klassentypen geladen werden müssten.
    </span>
   </p></blockquote>

   <p class="para">
    Wenn zum Beispiel <code class="literal">A</code> und <code class="literal">B</code> Aliase von
    Klassen sind, dann bleibt <code class="literal">A|B</code> ein gültiger Union-Typ,
    auch wenn er entweder auf <code class="literal">A</code> oder <code class="literal">B</code>
    reduziert werden könnte. Bei <code class="code">B extends A {}</code> ist
    <code class="literal">A|B</code> ebenfalls ein gültiger Union-Typ, auch wenn er auf
    <code class="literal">A</code> reduziert werden kann.

    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">int</span><span style="color: #007700">|</span><span style="color: #0000BB">INT </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Unzulässig<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">bool</span><span style="color: #007700">|</span><span style="color: #0000BB">false </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Unzulässig<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">int</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Traversable </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Unzulässig<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">self</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Traversable </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Unzulässig<br /><br /></span><span style="color: #007700">use </span><span style="color: #0000BB">A </span><span style="color: #007700">as </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br />function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">A</span><span style="color: #007700">|</span><span style="color: #0000BB">B </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Unzulässig ("use" ist Teil der Namensauflösung)<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">A</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">B </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Unzulässig ("use" ist Teil der Namensauflösung)<br /><br /></span><span style="color: #0000BB">class_alias</span><span style="color: #007700">(</span><span style="color: #DD0000">'X'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Y'</span><span style="color: #007700">);<br />function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">X</span><span style="color: #007700">|</span><span style="color: #0000BB">Y </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Zulässig (Redundanz ist nur zur Laufzeit bekannt)<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">X</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Y </span><span style="color: #007700">{} </span><span style="color: #FF8000">// Zulässig (Redundanz ist nur zur Laufzeit bekannt)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
  </div>
 </div>

 <div class="sect2" id="language.types.declarations.examples">
  <h3 class="title">Beispiele</h3>
  <div class="example" id="example-3">
   <p><strong>Beispiel #3 Grundlegendes Beispiel für die Deklaration eines Klassentyps</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br />class </span><span style="color: #0000BB">D </span><span style="color: #007700">extends </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">// Das erweitert C nicht.<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">E </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">C $c</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">D</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">E</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt mit PHP 8 folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
C
D

Fatal error: Uncaught TypeError: f(): Argument #1 ($c) must be of type C, E given, called in /in/gLonb on line 14 and defined in /in/gLonb:8
Stack trace:
#0 -(14): f(Object(E))
#1 {main}
  thrown in - on line 8
</pre></div>
   </div>
  </div>

  <div class="example" id="example-4">
   <p><strong>Beispiel #4 Grundlegendes Beispiel für die Deklaration eines Schnittstellentyps</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface </span><span style="color: #0000BB">I </span><span style="color: #007700">{ public function </span><span style="color: #0000BB">f</span><span style="color: #007700">(); }<br />class </span><span style="color: #0000BB">C </span><span style="color: #007700">implements </span><span style="color: #0000BB">I </span><span style="color: #007700">{ public function </span><span style="color: #0000BB">f</span><span style="color: #007700">() {} }<br /><br /></span><span style="color: #FF8000">// Das implementiert I nicht.<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">E </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">I $i</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">E</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt mit PHP 8 folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
C

Fatal error: Uncaught TypeError: f(): Argument #1 ($i) must be of type I, E given, called in - on line 13 and defined in -:8
Stack trace:
#0 -(13): f(Object(E))
#1 {main}
  thrown in - on line 8
</pre></div>
   </div>
  </div>

  <div class="example" id="example-5">
   <p><strong>Beispiel #5 Grundlegendes Beispiel für die Deklaration eines Rückgabetyps</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #0000BB">float </span><span style="color: #007700">{<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Hier wird ein Gleitkommawert zurückgegeben.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
float(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-6">
   <p><strong>Beispiel #6 Zurückgeben eines Objekts</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">getC</span><span style="color: #007700">(): </span><span style="color: #0000BB">C </span><span style="color: #007700">{<br />    return new </span><span style="color: #0000BB">C</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">getC</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
object(C)#1 (0) {
}
</pre></div>
   </div>
  </div>

  <div class="example" id="example-7">
   <p><strong>Beispiel #7 Deklaration eines nulllable Parameters</strong></p>
   <div class="example-contents">
    <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br />function </span><span style="color: #0000BB">f</span><span style="color: #007700">(?</span><span style="color: #0000BB">C $c</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
    <div class="annotation-interactive cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
   </div>
  </div>

  <div class="example" id="example-8">
   <p><strong>Beispiel #8 Deklaration eines nulllable Rückgabetyps</strong></p>
   <div class="example-contents">
    <div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">get_item</span><span style="color: #007700">(): ?</span><span style="color: #0000BB">string </span><span style="color: #007700">{<br />    if (isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'item'</span><span style="color: #007700">])) {<br />        return </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'item'</span><span style="color: #007700">];<br />    } else {<br />        return </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-9">
   <p><strong>Beispiel #9 Deklaration eines Klasseneigenschafts-Typs</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">User </span><span style="color: #007700">{<br />    public static </span><span style="color: #0000BB">string $foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br />    public </span><span style="color: #0000BB">int $id</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">string $username</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">int $id</span><span style="color: #007700">, </span><span style="color: #0000BB">string $username</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id </span><span style="color: #007700">= </span><span style="color: #0000BB">$id</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>

  
 <div class="sect2" id="language.types.declarations.strict">
  <h3 class="title">Strikte Typisierung</h3>

  <p class="para">
   Standardmäßig wandelt PHP Werte eines falschen Typs automatisch in den
   erwarteten Typ um, wenn dies möglich ist. Wenn an eine Funktion zum
   Beispiel ein Wert vom Typ <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> für einen Parameter übergeben
   wird, der einen Wert vom Typ <span class="type"><a href="language.types.string.php" class="type string">string</a></span> erwartet, dann erhält die
   Funktion eine Variable vom Typ <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
  </p>

  <p class="para">
   Der strikte Modus kann auf Dateibasis aktiviert werden. Im strikten Modus
   wird nur ein Wert akzeptiert, der genau der Typdeklaration entspricht,
   andernfalls wird ein <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> geworfen. Die einzige
   Ausnahme von dieser Regel ist die Übergabe eines <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>-Wertes,
   wenn ein <span class="type"><a href="language.types.float.php" class="type float">float</a></span>-Wert erwartet wird.
  </p>

  <div class="warning"><strong class="warning">Warnung</strong>
   <p class="simpara">
    Funktionsaufrufe innerhalb interner Funktionen sind von der
    <code class="literal">strict_types</code>-Deklaration nicht betroffen.
   </p>
  </div>

  <p class="para">
   Um den strikten Modus zu aktivieren, wird die <a href="control-structures.declare.php" class="link"><code class="literal">declare</code></a>-Anweisung zusammen
   mit der <code class="literal">strict_types</code>-Deklaration verwendet:
  </p>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Strikte Typisierung gilt für Funktionsaufrufe, die
    <em>innerhalb</em> einer Datei mit aktivierter strikter
    Typisierung gemacht werden, nicht für die Funktionen, die innerhalb dieser
    Datei deklariert sind. Wenn eine Funktion, die in einer Datei mit strikter
    Typisierung definiert ist, aus einer Datei aufgerufen wird, in der die
    strikte Typisierung nicht aktiviert ist, wird die Präferenz des Aufrufers
    (automatische Umwandlung des Typs, coercive typing) berücksichtigt und der
    Wert wird typkonvertiert.
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Die strikte Typisierung ist nur für skalare Typdeklarationen definiert.
   </p>
  </p></blockquote>

  <div class="example" id="example-10">
   <p><strong>Beispiel #10 Strikte Typisierung für die Werte von Argumenten</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int $a</span><span style="color: #007700">, </span><span style="color: #0000BB">int $b</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">, </span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt mit PHP 8 folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
int(3)

Fatal error: Uncaught TypeError: sum(): Argument #1 ($a) must be of type int, float given, called in - on line 9 and defined in -:4
Stack trace:
#0 -(9): sum(1.5, 2.5)
#1 {main}
  thrown in - on line 4
</pre></div>
   </div>
  </div>

  <div class="example" id="example-11">
   <p><strong>Beispiel #11 Erzwungene Typisierung für die Werte von Argumenten</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int $a</span><span style="color: #007700">, </span><span style="color: #0000BB">int $b</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Diese werden in Ganzzahlen umgewandelt: man beachte die folgende Ausgabe!<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">, </span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
int(3)
int(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-12">
   <p><strong>Beispiel #12 Strikte Typisierung für Rückgabewerte</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #0000BB">int </span><span style="color: #007700">{<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
int(3)

Fatal error: Uncaught TypeError: sum(): Return value must be of type int, float returned in -:5
Stack trace:
#0 -(9): sum(1, 2.5)
#1 {main}
  thrown in - on line 5
</pre></div>
   </div>
  </div>
 </div>

</div><?php manual_footer($setup); ?>