<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'language.oop5.overloading.php',
    1 => '&Uuml;berladung',
    2 => '&Uuml;berladung',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Klassen und Objekte',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.anonymous.php',
    1 => 'Anonyme Klassen',
  ),
  'next' => 
  array (
    0 => 'language.oop5.iterations.php',
    1 => 'Objektiteration',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'language/oop5/overloading.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.overloading" class="sect1">
  <h2 class="title">Überladung</h2>

  <p class="para">
   Überladung bietet in PHP Möglichkeiten, um dynamisch
   Eigenschaften und Methoden zu <q class="quote">erzeugen</q>.
   Diese dynamisch erzeugten Entitäten werden unter
   Zuhilfenahme von magischen Methoden verarbeitet,
   die man in einer Klasse zu verschiedenen Aktivitäten
   definieren kann.
  </p>

  <p class="para">
   Die Überladungsmethoden werden aufgerufen, wenn mit
   Eigenschaften oder Methoden interagiert wird, die entweder
   nicht deklariert wurden oder im aktuellen Geltungsbereich
   nicht <a href="language.oop5.visibility.php" class="link">sichtbar</a>
   sind. Im Rest dieses Abschnitts werden die Begriffe
   <q class="quote">unzugreifbare Eigenschaft</q> und
   <q class="quote">unzugreifbare Methode</q> verwendet, um auf die
   Kombination von Deklaration und Sichtbarkeit zu verweisen.
  </p>

  <p class="para">
   Alle Überladungsmethoden müssen als <code class="literal">public</code>
   definiert sein.
  </p>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Keiner der Parameter dieser magischen Methoden kann
    <a href="functions.arguments.php#functions.arguments.by-reference" class="link">als
    Referenz</a> übergeben werden.
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Die Interpretation von <q class="quote">Überladung</q> weicht
    von den meisten objektorientierten Programmiersprachen
    ab. Traditionell bezeichnet Überladung die Möglichkeit
    mehrere Methoden mit gleichem Namen aber unterschiedlichen
    Anzahlen und Typen von Parametern zu definieren.
   </p>
  </p></blockquote>


  <div class="sect2" id="language.oop5.overloading.members">
   <h3 class="title">Überladung von Eigenschaften</h3>

   <div class="methodsynopsis dc-description" id="object.set">
    <span class="modifier">public</span> <span class="methodname"><strong>__set</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

   <div class="methodsynopsis dc-description" id="object.get"><span class="modifier">public</span> <span class="methodname"><strong>__get</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

   <div class="methodsynopsis dc-description" id="object.isset"><span class="modifier">public</span> <span class="methodname"><strong>__isset</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

   <div class="methodsynopsis dc-description" id="object.unset"><span class="modifier">public</span> <span class="methodname"><strong>__unset</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>


   <p class="para">
    <a href="language.oop5.overloading.php#object.set" class="link">__set</a> wird aufgerufen, wenn Daten in
    unzugreifbare (protected oder private) Eigenschaften geschrieben werden
    sollen.
   </p>

   <p class="para">
    <a href="language.oop5.overloading.php#object.get" class="link">__get</a> wird verwendet, um Daten aus
    unzugreifbaren (protected oder private) Eigenschaften zu lesen.
   </p>

   <p class="para">
    <a href="language.oop5.overloading.php#object.isset" class="link">__isset</a> wird aufgerufen, indem
    <span class="function"><a href="function.isset.php" class="function">isset()</a></span> oder <span class="function"><a href="function.empty.php" class="function">empty()</a></span>
    auf unzugreifbare (protected oder private) Eigenschaften angewendet wird.
   </p>

   <p class="para">
    <a href="language.oop5.overloading.php#object.unset" class="link">__unset</a> wird aufgerufen, wenn
    <span class="function"><a href="function.unset.php" class="function">unset()</a></span> für unzugreifbaren (protected oder private)
    Eigenschaften aufgerufen wird.
   </p>

   <p class="para">
    Der Parameter <var class="varname">$name</var> beinhaltet den Namen
    der Eigenschaft, mit der interagiert wird. Der Parameter
    <var class="varname">$value</var> der Funktion <a href="language.oop5.overloading.php#object.set" class="link">__set</a>
    spezifiziert den Wert den die Eigenschaft <var class="varname">$name</var>
    annehmen soll.
   </p>

   <p class="para">
    Überladung von Eigenschaften funktioniert nur im Kontext von
    Objekten. Diese magischen Methoden werden nicht aus einem
    statischen Kontext aufgerufen. Diese Methoden sollten daher
    nicht als <a href="language.oop5.static.php" class="link">static</a>
    deklariert werden. Eine Warnung wird ausgegeben,
    wenn eine dieser magischen Überladungsmethoden als
    <code class="literal">static</code> deklariert ist.
   </p>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     Der Rückgabewert von <a href="language.oop5.overloading.php#object.set" class="link">__set</a> wird, aufgrund
     der Behandlung des Zuweisungsoperators in PHP, ignoriert.
     Aus ähnlichen Gründen wird <a href="language.oop5.overloading.php#object.get" class="link">__get</a> nicht
     aufgerufen, wenn man Zuweisungen in etwa wie folgt verkettet:
     <code class="literal"><div class="annotation-interactive cdata"><pre> $a = $obj-&gt;b = 8; </pre></div></code>
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <p class="para">
     PHP ruft niemals eine überladene Methode von derselben überladenen
     Methode aus auf. Das bedeutet zum Beispiel, dass
     <code class="code">return $this-&gt;foo</code> innerhalb von
     <a href="language.oop5.overloading.php#object.get" class="link">__get()</a> <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> zurückgibt und ein
     <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> auslöst, wenn die Eigenschaft
     <code class="literal">foo</code> nicht definiert ist, anstatt
     <a href="language.oop5.overloading.php#object.get" class="link">__get()</a> ein zweites Mal aufzurufen. Es
     ist jedoch möglich, dass eine überladene Methode implizit eine andere
     überladene Methode aufruft (z. B. löst
     <a href="language.oop5.overloading.php#object.set" class="link">__set()</a> den Aufruf von
     <a href="language.oop5.overloading.php#object.get" class="link">__get()</a> aus).
    </p>
   </p></blockquote>

   <div class="example" id="example-1">
    <p><strong>Beispiel #1 
     Überladung von Eigenschaften mit den Methoden
     <a href="language.oop5.overloading.php#object.get" class="link">__get</a>, <a href="language.oop5.overloading.php#object.set" class="link">__set</a>,
     <a href="language.oop5.overloading.php#object.isset" class="link">__isset</a> und <a href="language.oop5.overloading.php#object.unset" class="link">__unset</a>
    </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">EigenschaftTest<br /></span><span style="color: #007700">{<br />    </span><span style="color: #FF8000">/**  Speicherplatz für überladene Daten.  */<br />    </span><span style="color: #007700">private </span><span style="color: #0000BB">$data </span><span style="color: #007700">= array();<br /><br />    </span><span style="color: #FF8000">/**  Überladung wird nicht bei deklarierten Eigenschaften benutzt.  */<br />    </span><span style="color: #007700">public </span><span style="color: #0000BB">$declared </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br />    </span><span style="color: #FF8000">/**  Überladung wird nur von außerhalb der Klasse angewendet.  */<br />    </span><span style="color: #007700">private </span><span style="color: #0000BB">$hidden </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__set</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #DD0000">"Setze '</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">' auf '</span><span style="color: #0000BB">$value</span><span style="color: #DD0000">'\n"</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">[</span><span style="color: #0000BB">$name</span><span style="color: #007700">] = </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">__get</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #DD0000">"Lese '</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">'\n"</span><span style="color: #007700">;<br />        if (</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">)) {<br />            return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">[</span><span style="color: #0000BB">$name</span><span style="color: #007700">];<br />        }<br /><br />        </span><span style="color: #0000BB">$trace </span><span style="color: #007700">= </span><span style="color: #0000BB">debug_backtrace</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(<br />            </span><span style="color: #DD0000">'Undefinierte Eigenschaft für __get(): ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$name </span><span style="color: #007700">.<br />            </span><span style="color: #DD0000">' in ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$trace</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'file'</span><span style="color: #007700">] .<br />            </span><span style="color: #DD0000">' Zeile ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$trace</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'line'</span><span style="color: #007700">],<br />            </span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">);<br />        return </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">__isset</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #DD0000">"Ist '</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">' gesetzt?\n"</span><span style="color: #007700">;<br />        return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">[</span><span style="color: #0000BB">$name</span><span style="color: #007700">]);<br />    }<br /><br />    public function </span><span style="color: #0000BB">__unset</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #DD0000">"Lösche '</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">'\n"</span><span style="color: #007700">;<br />        unset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">[</span><span style="color: #0000BB">$name</span><span style="color: #007700">]);<br />    }<br /><br />    </span><span style="color: #FF8000">/**  Keine magische Methode, nur beispielhaft hier.  */<br />    </span><span style="color: #007700">public function </span><span style="color: #0000BB">getHidden</span><span style="color: #007700">()<br />    {<br />        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hidden</span><span style="color: #007700">;<br />    }<br />}<br /><br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">EigenschaftTest</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">a </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">a</span><span style="color: #007700">));<br />unset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">a</span><span style="color: #007700">));<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">declared </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Wir experimentieren nun mit der private-Eigenschaft 'hidden':\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Private ist innerhalb der Klasse sichtbar, also wird __get() nicht benutzt...\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getHidden</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Private nicht sichtbar von außerhalb der Klasse, also wird __get() benutzt...\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hidden </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</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>
Setze &#039;a&#039; auf &#039;1&#039;
Lese &#039;a&#039;
1

Ist &#039;a&#039; gesetzt?
bool(true)
Lösche &#039;a&#039;
Ist &#039;a&#039; gesetzt?
bool(false)

1

Wir experimentieren nun mit der private-Eigenschaft &#039;hidden&#039;:
Private ist innerhalb der Klasse sichtbar, also wird __get() nicht benutzt...
2
Private nicht sichtbar von außerhalb der Klasse, also wird __get() benutzt...
Lese &#039;hidden&#039;


Notice:  Undefinierte Eigenschaft für __get(): hidden in &lt;file&gt; Zeile 70 in &lt;file&gt; on line 29
</pre></div>
    </div>

   </div>
  </div>

  <div class="sect2" id="language.oop5.overloading.methods">
   <h3 class="title">Überladung von Methoden</h3>

   <div class="methodsynopsis dc-description" id="object.call">
    <span class="modifier">public</span> <span class="methodname"><strong>__call</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$arguments</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

   <div class="methodsynopsis dc-description" id="object.callstatic"><span class="modifier">public static</span> <span class="methodname"><strong>__callStatic</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$arguments</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>


   <p class="para">
    <a href="language.oop5.overloading.php#object.call" class="link">__call</a> wird aufgerufen, wenn eine unzugreifbare
    Methode in einem Objekt aufgerufen wird.
   </p>

   <p class="para">
    <a href="language.oop5.overloading.php#object.callstatic" class="link">__callStatic</a> wird aufgerufen, wenn eine
    unzugreifbare Methode in einem statischen Kontext aufgerufen wird.
   </p>

   <p class="para">
    Der Parameter <var class="varname">$name</var> ist der Name der aufgerufenen
    Methode. Der Parameter <var class="varname">$arguments</var> beinhaltet ein
    Array mit den Parametern, die der Methode <var class="varname">$name</var>
    übergeben wurden.
   </p>

   <div class="example" id="example-2">
    <p><strong>Beispiel #2 
     Überladung von Methoden mit den methoden <a href="language.oop5.overloading.php#object.call" class="link">__call</a>
     und <a href="language.oop5.overloading.php#object.callstatic" class="link">__callStatic</a>
    </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">MethodenTest </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__call</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$arguments</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #FF8000">// Achtung: Der Wert von $name beachtet die Groß-/Kleinschreibung<br />        </span><span style="color: #007700">echo </span><span style="color: #DD0000">"Rufe die Objektmethode '</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">' "<br />             </span><span style="color: #007700">. </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">$arguments</span><span style="color: #007700">). </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    }<br /><br />    public static function </span><span style="color: #0000BB">__callStatic</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$arguments</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #FF8000">// Achtung: Der Wert von $name beachtet die Groß-/Kleinschreibung<br />        </span><span style="color: #007700">echo </span><span style="color: #DD0000">"Rufe die statische Methode '</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">' "<br />             </span><span style="color: #007700">. </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">$arguments</span><span style="color: #007700">). </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">MethodenTest</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">runTest</span><span style="color: #007700">(</span><span style="color: #DD0000">'eines Objektes auf'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">MethodenTest</span><span style="color: #007700">::</span><span style="color: #0000BB">runTest</span><span style="color: #007700">(</span><span style="color: #DD0000">'aus statischem Kontext auf'</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>
Rufe die Objektmethode &#039;runTest&#039; eines Objektes auf
Rufe die statische Methode &#039;runTest&#039; aus statischem Kontext auf
</pre></div>
    </div>
   </div>

  </div>

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