この記事ではオブジェクト指向の知識については詳しく説明しません。この記事では PHP のコンストラクターに焦点を当てます。
PHP クラスのコンストラクターには、マジック キューブ __construct() またはクラスと同じ名前の関数を使用できます。例は次のとおりです。
<code><span><span>class</span><span>A</span>{</span><span>public</span><span><span>function</span><span>A</span><span>()</span>{</span><span>echo</span><span>'A is constructing...'</span>; } } <span><span>class</span><span>B</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>'B is contructing...'</span>; } } <span>$a</span> = <span>new</span> A(); <span>// A is constructing...</span><span>$b</span> = <span>new</span> B(); <span>// B is constructing...</span></code>
さらに、継承する場合は、次の点に注意する必要があります。
[
サブクラスはコンストラクターを書く必要がないので、親クラスのコンストラクターを使用します]
<code><span><span>class</span><span>A</span>{</span><span>protected</span><span>$name</span>; <span>public</span><span><span>function</span><span>A</span><span>()</span>{</span><span>echo</span><span>'A is constructing...<br>'</span>; } <span>public</span><span><span>function</span><span>set_name</span><span>(<span>$name</span>)</span>{</span><span>$this</span>->name = <span>$name</span>; } <span>public</span><span><span>function</span><span>get_name</span><span>()</span>{</span><span>return</span><span>$this</span>->name; } } <span><span>class</span><span>B</span><span>extends</span><span>A</span>{</span><span>/* public function __construct(){ echo 'B is contructing...<br>'; } */</span> } <span>//$a = new A();</span><span>$b</span> = <span>new</span> B(); <span>// A is constructing...</span><span>$b</span>->set_name(<span>'zhangsan'</span>); <span>echo</span><span>$b</span>->get_name();</code>
サブクラスがコンストラクターを作成した場合、親クラスのコンストラクターは再び呼び出されません]
<code><span><span>class</span><span>A</span>{</span><span>protected</span><span>$name</span>; <span>public</span><span><span>function</span><span>A</span><span>()</span>{</span><span>echo</span><span>'A is constructing...<br>'</span>; } <span>public</span><span><span>function</span><span>set_name</span><span>(<span>$name</span>)</span>{</span><span>$this</span>->name = <span>$name</span>; } <span>public</span><span><span>function</span><span>get_name</span><span>()</span>{</span><span>return</span><span>$this</span>->name; } } <span><span>class</span><span>B</span><span>extends</span><span>A</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>'B is contructing...<br>'</span>; } } <span>//$a = new A();</span><span>$b</span> = <span>new</span> B(); <span>// just echo 'B is contructing...'</span><span>$b</span>->set_name(<span>'zhangsan'</span>); <span>echo</span><span>$b</span>->get_name(); <span>// zhangsan</span></code>
親クラスのコンストラクタがprivateであれば継承可能ですが、サブクラスは独自のコンストラクタを持たせて明確に記述する必要があります】
<code><span><span>class</span><span>A</span>{</span><span>protected</span><span>$name</span>; <span>private</span><span><span>function</span><span>A</span><span>()</span>{</span><span>echo</span><span>'A is constructing...<br>'</span>; } <span>public</span><span><span>function</span><span>set_name</span><span>(<span>$name</span>)</span>{</span><span>$this</span>->name = <span>$name</span>; } <span>public</span><span><span>function</span><span>get_name</span><span>()</span>{</span><span>return</span><span>$this</span>->name; } } <span><span>class</span><span>B</span><span>extends</span><span>A</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>'B is contructing...<br>'</span>; } } <span>//$a = new A();</span><span>$b</span> = <span>new</span> B(); <span>// B is contructing...</span><span>$b</span>->set_name(<span>'zhangsan'</span>); <span>echo</span><span>$b</span>->get_name(); <span>// zhangsan</span></code>