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

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

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool