Home Backend Development PHP Tutorial PHP的构造方法,析构方法和this关键字详细介绍_PHP

PHP的构造方法,析构方法和this关键字详细介绍_PHP

Jun 01, 2016 am 11:59 AM
this Construction method Destructor method

一.什么是构造方法
    构造方法是类的一种特殊的方法,它的主要作用是完成对新对象初始化.
    特点:
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 "不带参数的构造方法";

        /

Statement of this Website
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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1249
24
Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

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!

Do php traits support constructors? Do php traits support constructors? Mar 22, 2023 pm 04:54 PM

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.

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

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.

Clever way to use this keyword in jQuery Clever way to use this keyword in jQuery Feb 25, 2024 pm 04:09 PM

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? An in-depth analysis of this in JavaScript What is this? An in-depth analysis of this in JavaScript Aug 04, 2022 pm 05:02 PM

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? Brief analysis of three methods How does JavaScript change this pointer? Brief analysis of three methods Sep 19, 2022 am 09:57 AM

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 Constructor Property Promotion in PHP8 to improve code maintainability? How to use Constructor Property Promotion in PHP8 to improve code maintainability? Oct 19, 2023 am 11:34 AM

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

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

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

See all articles