Kategorie PHP

PHP 7 anonyme Klassen

Dieser Artikel zeigt, wie man in PHP 7 anonyme Klassen deklariert und was man damit anstellen kann.

Eine anonyme Klasse wird gleichzeitig deklariert und instanziiert. Sie hat keinen Bezeichner, allerdings alle Eigenschaften einer normalen Klasse. Anonyme Klassen sind sinnvoll, wenn eine einzigartige und einmalige Aufgabe zu erfüllen ist, ohne gleich den Overhead einer ganzen Klasse zu erzeugen. Dazu gleich ein Beispiel:


<span class="token variable">$name</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">class</span><span class="token punctuation">(</span><span class="token string">'Hans Mustermann'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span>string <span class="token variable">$name</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token variable">$name</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
PHP

Wie in dem Beispiel zu sehen, erfüllt eine anonyme Klasse alle Erfordernisse einer normalen Klasse, außer dass sie keinen Bezeichner trägt und nicht mehrfach genutzt wird.

Anonyme Klassen können andere Klassen erweitern und haben die gleiche Eltern-Kind Beziehungen wie normale, bezeichnete Klassen. Auch hierzu ein Beispiel:


<span class="token keyword">class</span> <span class="token class-name">Basisklasse</span>
<span class="token punctuation">{</span>
  <span class="token keyword">protected</span> <span class="token variable">$number</span><span class="token punctuation">;</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> 'Ich bin der Konstruktor der Basisklasse<span class="token punctuation">.</span>"<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getNumber</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> float
  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">number</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token variable">$number</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">class</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token keyword">extends</span> <span class="token class-name">Basisklasse</span>
<span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span>float <span class="token variable">$number</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
    <span class="token keyword">parent</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">number</span> <span class="token operator">=</span> <span class="token variable">$number</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">echo</span> <span class="token variable">$number</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getNumber</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
PHP

Führt man den Beispielcode aus, so erhält man als Ausgabe


Ich bin der Konstruktor der Basisklasse.
5

In dem Beispiel wurde die Klasse Basisklasse erweitert, wie das in einer normalen Klasse geschieht. Ferner können public und protected Eigenschaften und Methoden innerhalb von anonymen Klassen angesprochen werden.

Anonyme Klassen können auch Interfaces implementieren. Auch dazu gleich ein Beispiel:


<span class="token keyword">interface</span> <span class="token class-name">Infos</span>
<span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span>string <span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$string</span> <span class="token variable">$address</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getAddress</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">Basisklasse</span>
<span class="token punctuation">{</span>
  <span class="token keyword">protected</span> <span class="token variable">$number</span><span class="token punctuation">;</span>
  <span class="token keyword">protected</span> <span class="token variable">$name</span><span class="token punctuation">;</span>
  <span class="token keyword">protected</span> <span class="token variable">$address</span><span class="token punctuation">;</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> 'Ich bin der Konstruktor der Basisklasse<span class="token punctuation">.</span>"<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getNumber</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> float
  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">number</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token variable">$info</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">class</span><span class="token punctuation">(</span><span class="token string">'Hans Mustermann'</span><span class="token punctuation">,</span> <span class="token string">'Musterstraße 10, 12345 Musterstadt'</span><span class="token punctuation">)</span> <span class="token keyword">extends</span> <span class="token class-name">Basisklasse</span> <span class="token keyword">implements</span> <span class="token class-name">Info</span>
<span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span>string <span class="token variable">$name</span><span class="token punctuation">,</span> string <span class="token variable">$address</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
    <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span> <span class="token operator">=</span> <span class="token variable">$name</span><span class="token punctuation">;</span>
    <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">address</span> <span class="token operator">=</span> <span class="token variable">$address</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> string
  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getAddress</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> string
  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">address</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token variable">$info</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token string">' '</span> <span class="token punctuation">.</span> <span class="token variable">$info</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getAddress</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
PHP

In diesem Beispiel wurde das Interface Info hinzugefügt und die Klasse Basisklasse um die Instanzen $name und $address erweitert. In der anonymen Klasse wurden zunächst die Instanzen mit Eingabeparametern befüllt und mit den Methoden getName() und getAddress() Schnittstellen geschaffen, um die Instanzvariablen von außen zugänglich zu machen.

Schließlich lassen sich anonyme Klassen auch innerhalb anderer Klassen verwenden. Auch hierzu noch ein Beispiel:


<span class="token keyword">class</span> <span class="token class-name">Math</span>
<span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token variable">$first_number</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
  <span class="token keyword">public</span> <span class="token variable">$second_number</span> <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span><span class="token punctuation">:</span> float
  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">first_number</span> <span class="token operator">+</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">second_number</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">multiply_sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">class</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">extends</span> <span class="token class-name">Math</span>
    <span class="token punctuation">{</span>
      <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">multiply</span><span class="token punctuation">(</span>float <span class="token variable">$third_number</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> float
      <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token variable">$third_number</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token variable">$math</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Math</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$math</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">multiply_sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">multiply</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
PHP

Der Beispielcode liefert als Ergebnis 60. Zunächst wird die Methode multiply_sum() ausgeführt, welche als Rückgabewert eine anonyme Klasse verwendet. In dieser wird die Methode multiply() verwendet, die wiederum die Methode add() aus der Klasse Math ausführt und das Ergebnis mit dem Wert des Übergabeparameters – hier 2 – multipliziert.

Weitere Einzelheiten zu anonymen Klassen in PHP liefert die PHP Dokumentation.

Ähnliche Artikel:

Fragen oder Feedback zu diesem Artikel

Deine E-Mail-Adresse wird nicht veröffentlicht.