本文不再贅述物件導向的知識,本文著重說明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>
【父類別的建構子如果是私有的,可以被繼承,但是子類別必須有自己的建構函數,並且明確寫出來】
<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>