Home  >  Article  >  Backend Development  >  PHP constructor and destructor Constructor destructor oc destructor c destructor

PHP constructor and destructor Constructor destructor oc destructor c destructor

WBOY
WBOYOriginal
2016-07-29 08:52:43938browse

The PHP constructor is the first method automatically called after the object is created, and the destructor is the last method automatically called before the object is released. This article introduces PHP constructors and destructors to programmers.

php constructor
1. It is the "first" "automatically called" method after the object is created
2. The definition of the constructor method, the method name is fixed,
In php4: the same as the class name The method is the construction method
In php5: The construction method chooses to use the magic method __construct(). This name is used to declare the constructor method in all classes
Advantage: When the class name is changed, the construction method does not need to be changed
Magic method: Write in the class When a certain magic method is created, the function corresponding to this method will be added.
The method names are all fixed (all provided by the system), and there are no self-defined ones.
Each magic method is used to complete a certain task at different times. A method that is automatically called by a function
Different magic methods have different calling timings
All methods start with __
__construct(); __destruct(); __set();...
Function: as member attributes Initialization;

php destructor

1. The last "automatically" called method before the object is released
Use the garbage collector (java php), while c++ manual release
Function: close some resources and do some cleanup Work

__destruct();

php constructor and destructor examples

<span>class</span><span> Person{ 
</span><span>var</span><span>$name</span><span>; 
</span><span>var</span><span>$age</span><span>; 
</span><span>var</span><span>$sex</span><span>; 
</span><span>//</span><span>php4中的构造方法 </span><span>/*</span><span>function Person() 
{ 
//每声明一个对象都会调用 
echo "1111111111111111"; 
}</span><span>*/</span><span>//</span><span>php5中的构造方法 </span><span>function</span> __construct(<span>$name</span>,<span>$age</span>,<span>$sex</span><span>){ 
</span><span>$this</span>->name=<span>$name</span><span>; 
</span><span>$this</span>->age=<span>$age</span><span>; 
</span><span>$this</span>->sex=<span>$sex</span><span>; 
} 
</span><span>function</span><span> say(){ 
</span><span>//</span><span>$this->name;//对象中成员的访问使用$this </span><span>echo</span> "我的名字:{<span>$this</span>->name},我的年龄:{<span>$this</span>->age}<br>"<span>
} 
</span><span>function</span><span> run(){ 
} 
</span><span>function</span><span> eat(){ 
} 
</span><span>//</span><span>析构方法 </span><span>function</span><span> __destruct(){ 
} 
} 
</span><span>$p1</span>=<span>new</span> Person("zhangsan",25,"男"<span>); 
</span><span>$p2</span>=<span>new</span><span> Person; 
</span><span>$p3</span>=<span>new</span> Person; 

Original address: http://www.manongjc.com/article/730.html

Related reading:

php analysis Simple instructions for using the constructor

A simple example of PHP initialization object constructor and destructor

Detailed introduction of PHP constructor, destructor and this keyword

PHP constructor and destructor analysis

The above introduces the PHP constructor and destructor, including the destructor and constructor. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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