PHP object-oriented constructor and destructor

不言
Release: 2023-03-29 22:10:02
Original
1420 people have browsed it

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');
Copy after login

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;;
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!