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:43846browse

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

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