PHP的构造方法,析构方法和this关键字详细介绍_PHP
一.什么是构造方法
构造方法是类的一种特殊的方法,它的主要作用是完成对新对象初始化.
特点:
1. 没有返回值.
2. 在创建一个新的对象时,系统会自动调用该类的构造方法完成对新对角的初始化.
语法:
php5: 修饰符 function __construct()
{
//code
}
php4: 修饰符 function 类名()
{
//code
}
注意:
1. php5里对两者都支持,如果两种构造方法同时存在的话,优先选择第一种.
2. 一个类里面默认有一个不带参数为空的构造方法,一旦自定义了一个构造方法,就会覆盖默认的构造方法.
所以说一个类有且只有一个构造方法.
3.一个类只能有一个构造方法.(不能重载)
4.构造方法默认的访问修饰符为public.
二.this关键字
this代表当前对象.可以理解为:谁调用它,它就代表谁.
注意事项:
this不在类定义的使用,只能在类定义的方法中使用.
三.实例
复制代码 代码如下:
header("Conter-Type:text/html;charset=utf-8");
class Person
{
public $name; //成员变量
public $age;
// function __construct()
//{
// echo "不带参数的构造方法";
/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

PHP Traits does not support constructors. Because Traits cannot be instantiated individually, they can only be referenced by classes. Since Trait itself does not have a constructor, it is impossible to define a constructor in Trait.

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Flexible use of this keyword in jQuery In jQuery, the this keyword is a very important and flexible concept. It is used to refer to the DOM element currently being manipulated. By rationally using this keyword, we can easily operate elements on the page and achieve various interactive effects and functions. This article will combine specific code examples to introduce the flexible use of this keyword in jQuery. Simple this example First, let's look at a simple this example. Suppose we have a

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

How does JavaScript change this pointer? The following article will introduce to you three methods of changing this pointer in JS. I hope it will be helpful to you!

How to use ConstructorPropertyPromotion in PHP8 to improve code maintainability? With the release of PHP8, we ushered in some new language features. One of them is ConstructorPropertyPromotion (a shorthand for constructor properties). This feature makes it easier for us to define and initialize class properties, thereby improving the readability and maintainability of the code. This article will introduce Constructor

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye
