我们在创建和销毁对象时需要执行一些任务。例如,在创建对象时给属性赋值,在对象销毁时关闭数据连接等,这时就需要构造函数和析构函数。
在PHP中构造函数和析构函数是固定的,如下:
<font face="微软雅黑"><span style="color: #008000">//</span><span style="color: #008000"> 构造函数</span> <span style="color: #0000ff">function</span> __construct([argument1,argument2,...,</font><font face="微软雅黑"><span style="color: #000000">argumentN]){ </span><span style="color: #008000">/*</span><span style="color: #008000"> Class initialization code </span><span style="color: #008000">*/</span></font><span style="color: #000000"> <font face="微软雅黑">} </font></span><font face="微软雅黑"><span style="color: #008000">//</span><span style="color: #008000"> 析构函数</span> <span style="color: #0000ff">function</span></font><font face="微软雅黑"><span style="color: #000000"> __destruct(){ </span><span style="color: #008000">//</span><span style="color: #008000"> ...</span> }</font>
构造函数使用例子:
<span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> Person{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$age</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$gender</span><span style="color: #000000">; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$age</span>,<span style="color: #800080">$gender</span><span style="color: #000000">){ </span><span style="color: #800080">$this</span>->setName(<span style="color: #800080">$name</span><span style="color: #000000">); </span><span style="color: #800080">$this</span>->setAge(<span style="color: #800080">$age</span><span style="color: #000000">); </span><span style="color: #800080">$this</span>->setGender(<span style="color: #800080">$gender</span><span style="color: #000000">); } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> setName(<span style="color: #800080">$name</span><span style="color: #000000">){ </span><span style="color: #800080">$this</span>->name = <span style="color: #800080">$name</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> ... getter setter 方法</span> <span style="color: #000000"> } </span><span style="color: #800080">$peron</span> = <span style="color: #0000ff">new</span> Person("lee",18,'男'<span style="color: #000000">); </span>?>
调用父类构造函数、调用无关类的构造函数:
<span style="color: #0000ff">function</span><span style="color: #000000"> __construct(){ parent</span>::__construct(); <span style="color: #008000">//</span><span style="color: #008000"> 调用父类的构造函数必须显示的使用parent调用父类构造函数</span> classname::__construct(); <span style="color: #008000">//</span><span style="color: #008000"> 调用其他类的构造函数,classname是类名 //其他操作</span> }
PHP中的子类的构造函数不会主动调用父类的构造函数,要显示的使用parent::__construct()调用:
<span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> Animal{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span><span style="color: #000000">){ </span><span style="color: #800080">$this</span>->setName(<span style="color: #800080">$name</span><span style="color: #000000">) </span><span style="color: #0000ff">echo</span> "动物类被创建!"<span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> ... 其他方法</span> <span style="color: #000000"> } </span><span style="color: #0000ff">class</span> Birds <span style="color: #0000ff">extends</span><span style="color: #000000"> Animal{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$leg</span><span style="color: #000000">; </span><span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$leg</span><span style="color: #000000">){ parent</span>::__construct(<span style="color: #800080">$name</span>); <span style="color: #008000">//</span><span style="color: #008000"> 显示调用</span> <span style="color: #800080">$this</span>->setLeg(<span style="color: #800080">$leg</span><span style="color: #000000">); </span><span style="color: #0000ff">echo</span> "鸟类被创建!"<span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> ... 其他方法</span> <span style="color: #000000">} </span>?>
如果涉及多层继承,当调用parent::__construct()时,会沿着父类向上搜索,直到找到最合适的构造函数,例如:
<span style="color: #008000">//</span><span style="color: #008000"> 接上例</span> <span style="color: #0000ff">class</span> Parrot <span style="color: #0000ff">extends</span><span style="color: #000000"> Birds{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$leg</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$wing</span><span style="color: #000000">; </span><span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span><span style="color: #000000">){ parent</span>::__construct(<span style="color: #800080">$name</span>); <span style="color: #008000">//</span><span style="color: #008000"> 此时没有找到父类(Birds类)合适的构造函数,只能向上搜索,搜索到Animal类时,才找到合适的构造函数</span> <span style="color: #0000ff">echo</span> "鹦鹉类被创建!"<span style="color: #000000">; </span><span style="color: #800080">$this</span>-><span style="color: #000000">smackTalk(); </span><span style="color: #008000">/*</span><span style="color: #008000"> 输出结果: "动物类被创建!" "鹦鹉说话!" </span><span style="color: #008000">*/</span><span style="color: #000000"> } </span><span style="color: #0000ff">function</span><span style="color: #000000"> smackTalk(){ </span><span style="color: #0000ff">echo</span> "鹦鹉说话!"<span style="color: #000000">; } }</span>
如果想要依次调用几个父类的构造函数,可以使用类名直接调用构造函数,例如:
<span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$leg</span><span style="color: #000000">){ Animal</span>::__construct(<span style="color: #800080">$name</span>); <span style="color: #008000">//</span><span style="color: #008000"> 调用Animal构造函数</span> Birds::__construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$leg</span>); <span style="color: #008000">//</span><span style="color: #008000"> 调用Birds构造函数</span> }
PHP是不支持函数的重载,所有构造函数同样不能有多个。
在以下几种情况下可能会调用析构函数(但不一定):
PHP的内存回收机制和JAVA的很类似,对没有任何引用的对象进行销毁回收,采用引用计数器的技术。
例子:
<span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> test{ </span><span style="color: #0000ff">function</span><span style="color: #000000"> __destruct(){ </span><span style="color: #0000ff">echo</span> "当对象销毁时会调用!!!"<span style="color: #000000">; } } </span><span style="color: #800080">$a</span> = <span style="color: #800080">$b</span> = <span style="color: #800080">$c</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> test(); </span><span style="color: #800080">$a</span> = <span style="color: #0000ff">null</span><span style="color: #000000">; </span><span style="color: #0000ff">unset</span>(<span style="color: #800080">$b</span><span style="color: #000000">); </span><span style="color: #0000ff">echo</span> "<hr>"<span style="color: #000000">; </span>?>
此例子,如下图,有三个变量引用$a,$b,$c指向test对象,test对象就有3个引用计数,当$a = null时,$a对test对象的引用丢失,计数-1,变为2,当$b被unset()时,$b对test对象的引用也丢失了,计数再-1,变为1,最后页面加载完毕,$c指向test对象的引用自动被释放,此时计数再-1,变为0,test对象已没有变量引用,就会被销毁,此时就会调用析构函数。