Home php教程 php手册 PHP的构造方法,析构方法和this关键字详细介绍

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

Jun 13, 2016 am 09:31 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 "不带参数的构造方法";

        //}
        function __construct($name,$age)
        {
            $this -> name = $name;
            $this -> age = $age;
            echo "带参数的构造方法"."
";
        }
        //成员方法
        function view()
        {
            //this的引用.
            echo "姓名:".$this ->name.", 年龄:".$this ->age;

        }
    }
        //new一个新的对象
    //$p = new Person();
    $p2 = new Person("李四",13);
    $p2 ->view();
?>

结果如下:
    带参数的构造方法

复制代码 代码如下:


    姓名:李四, 年龄:13

四:析构方法:
    析构方法是PHP5引入的新概念.主要作用:释放资源(比如:释放数据库链接,图片资源...).
    语法:
    function __destruct(){}
    特点:

    1.析构方法没有返回值.

    2.主要作用是释放资源.并不是销毁对象本身.
    3.在销毁对象前,系统自动调用该类的析构方法.

    4.一个类最多只有一个析构方法.

五:例子:

复制代码 代码如下:


    header("Conter-Type:text/html;charset=utf-8");

    class Person
    {
        public $name;
        public $age;
        //构造方法
        function __construct($name,$age)
        {
            $this ->name = $name;
            $this ->age = $age; 

        }
        //析构方法
        function __destruct()
        {
            echo "姓名:".$this->name.", 年龄".$this->age."-->销毁
";
        }

    }

    $p1= new Person("小一",18);
    $p2= new Person("小二",17);
?>

结果:
    姓名:小二, 年龄17-->销毁
    姓名:小一, 年龄18-->销毁

分析结论:
    1.析构方法会自动调用.

    2.析构方法调用的顺序是先创建的对象后被销毁.

    3.当一个对象没有引用,被垃圾回收机制确认为垃圾时,调用析构方法.

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

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!

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.

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.

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

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!

Detailed explanation of this in JavaScript arrow function Detailed explanation of this in JavaScript arrow function Jan 25, 2024 pm 01:41 PM

The arrow function in JavaScript is a relatively new syntax. It does not have its own this keyword. On the contrary, the this of the arrow function points to the scope object containing it. The impacts are: 1. This in the arrow function is static; 2. Arrow Functions cannot be used as constructors; 3. Arrow functions cannot be used as methods.

See all articles