php constructor is the first method automatically called after the object is created, and the destructor is when the object is The last automatically called method before release. 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 A fixed one,
In php4: The method with the same name as the class is the constructor method
In php5: The constructor method is selected using the magic method __construct(). All constructor methods declared in the class use this name
Advantages: When changing the class name, the construction method does not need to be changed.
Magic method: When a certain magic method is written in the class, the function corresponding to this method will be added.
The method names are all fixed (all (provided by the system), there is no self-defined
Every magic method is a method that is automatically called at different times to complete a certain function.
Different magic methods have different calling timings.
They are all based on Methods starting with __
__construct(); __destruct(); __set();...
Function: Initialize member attributes;
php destructor
1. The last "automatically" called method before the object is released
Use the garbage collector (java php), and 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:
Simple instructions for using PHP destructor
A simple example of PHP initialization object constructor and destructor
Detailed introduction to PHP constructor, destructor and this keyword
php constructor and destructor analysis