search
Homephp教程php手册php学习笔记 面向对象的构造与析构方法

复制代码 代码如下:


/*
* 1.对象中成员的访问(在一个对象的内部方法中,去访问本对轩昂中的其他方法和成员属性)
* 2.在对象中的方法中都默认有一个$this关键字,这个关键字代表调用这个方法的对象
*
* 构造方法
*
* 1.是对象创建完成后,“第一个”“自动调用”的方法
*
* 2.构造方法的定义,方法名是一个固定的,
* 在php4中:和类名相同的方法就是构造方法
* 在php5中:构造方法选择使用 魔术方法__construct() 所有类中声明构造方法都使用这个名称
* 优点:在改变类名时,构造方法不用改变
* 魔术方法: 在类中写出了某个魔术方法,这个方法对应的功能就会添加上
* 方法名称都是固定的(都是系统提供好的),没有自己定义的
* 每一个魔术方法,都是在不同时刻为了完成某一功能自动调用的方法
* 不同的魔术方法有不同的调用时机
* 都是以 __ 开头的方法
* __construct(); __destruct(); __set();......
*
* 作用:为成员属性初始化;
*
*
* 析构方法
*
* 1.当对象被释放之前最后一个“自动”调用的方法
* 使用垃圾回收器(java php),而c++手动 的释放
*
* 作用:关闭一些资源,作一些清理的工作
*
* __destruct();
*
*/
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;



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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools