本文不再赘述面向对象的知识,本文着重说明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教程有兴趣的朋友有所帮助。