首頁 > 後端開發 > php教程 > PHP物件導向建構函數說明

PHP物件導向建構函數說明

WBOY
發布: 2016-07-30 13:30:03
原創
827 人瀏覽過

本文不再贅述物件導向的知識,本文著重說明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>
登入後複製
版權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了PHP物件導向建構函數說明,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板