Home  >  Article  >  Backend Development  >  PHP object-oriented constructor and destructor

PHP object-oriented constructor and destructor

不言
不言Original
2018-06-06 09:50:241424browse

This article mainly introduces the constructor and destructor of PHP object-oriented, which has certain reference value. Now I share it with you. Friends in need can refer to it

Constructor

For a class with a constructor, this method will be called every time it is instantiated, which is suitable for initialization work.

Example

class MyClass
{
    // 构造函数 
    public function __construct($str)
    {
        echo $str;
    }
}

// 实例化对象
$c1= new MyClass('abc');

Destructor

When all references to an object are deleted, or are explicitly destroyed, or when the program ends ,implement.

Example

<?php

class Myclass
{
    public function __destruct(){
        echo &#39;对象被销毁了&#39;;
    }
}

$obj = new MyClass();

// 销毁对象的2种方法
unset($obj);
$obj = null;

echo &#39;<hr>&#39;;

Related recommendations:

php object-oriented encapsulation

php object-oriented Classes and instantiated objects


The above is the detailed content of PHP object-oriented constructor and destructor. For more information, please follow other related articles on the PHP Chinese website!

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