search
Homephp教程php手册php构造函数与析构函数,php构造函数

php构造函数与析构函数,php构造函数

php构造函数是对象创建完成后,第一个自动调用的方法,析构函数是当对象被释放之前最后一个自动调用的方法。本文章向码农介绍php构造函数与析构函数。

 

php构造函数 

1.是对象创建完成后,“第一个”“自动调用”的方法 
2.构造方法的定义,方法名是一个固定的, 
在php4中:和类名相同的方法就是构造方法 
在php5中:构造方法选择使用 魔术方法__construct() 所有类中声明构造方法都使用这个名称 
优点:在改变类名时,构造方法不用改变 
魔术方法: 在类中写出了某个魔术方法,这个方法对应的功能就会添加上 
方法名称都是固定的(都是系统提供好的),没有自己定义的 
每一个魔术方法,都是在不同时刻为了完成某一功能自动调用的方法 
不同的魔术方法有不同的调用时机 
都是以 __ 开头的方法 
 __construct(); __destruct(); __set();...... 

作用:为成员属性初始化; 

 

php析构函数

1.当对象被释放之前最后一个“自动”调用的方法 
使用垃圾回收器(java php),而c++手动 的释放 
作用:关闭一些资源,作一些清理的工作 

__destruct(); 

 

php构造函数与析构函数实例

<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; 

原文地址:http://www.manongjc.com/article/730.html

 

相关阅读:

php析构函数的简单使用说明

php初始化对象构造函数和析构函数的简单实例

php构造函数,析构函数和this关键字详细介绍

php构造函数和析构函数解析

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.