PHP面向对象构造函数说明

WBOY
发布: 2016-07-30 13:30:03
原创
815 人浏览过

本文不再赘述面向对象的知识,本文着重说明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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板