Home > php教程 > php手册 > body text

php作用域问题和$this问题

WBOY
Release: 2016-06-06 19:42:25
Original
796 people have browsed it

疑问1 :_destruct析构函数(destructor) 与构造函数相反,当对象脱离其 作用 域时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做清理善后 的工作(例如在建立对象时用new开辟了一片内存空间,应在退出前在析构函数中用delete

疑问1:_destruct析构函数(destructor) 与构造函数相反,当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做“清理善后” 的工作(例如在建立对象时用new开辟了一片内存空间,应在退出前在析构函数中用delete释放)。

疑问2:this就是指向当前对象实例的指针,不指向任何其他对象或类。

代码注解:

php作用域问题和$this问题php作用域问题和$this问题代码

<p><span>class</span><span> UserName<br>{ <br>    </span><span>//</span><span>定义属性    </span><span><br></span><span>private</span><span>$name</span><span>;<br> <br>     </span><span>//</span><span>定义构造函数</span><span><br></span><span>function</span><span> __construct( </span><span>$name</span><span> )<br>     {<br>          </span><span>$this</span><span>-></span><span>name </span><span>=</span><span>$name</span><span>; </span><span>//</span><span>这里已经使用了this指针</span><span><br></span><span>     }<br> <br>     </span><span>//</span><span>析构函数</span><span><br></span><span>function</span><span> __destruct(){} <br><br>     </span><span>//</span><span>打印用户名成员函数</span><span><br></span><span>function</span><span> printName()<br>     {<br>          </span><span>print</span><span>( </span><span>$this</span><span>-></span><span>name ); </span><span>//</span><span>又使用了this指针</span><span><br></span><span>     }<br> } <br><br></span></p>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!