What is the difference between constructor and destructor in php

王林
Release: 2023-03-01 18:54:01
Original
4467 people have browsed it

The difference between constructors and destructors in php is: 1. The constructor can receive parameters and can be assigned to object properties when creating an object. The destructor cannot take parameters; 2. The constructor is called when creating an object. Function, the destructor is automatically called when the object is destroyed.

What is the difference between constructor and destructor in php

Difference analysis:

Constructor

Classes with a constructor will first call this method each time an object is created.

void __construct ([ mixed $args [, $... ]] )
Copy after login
  • The constructor can receive parameters and can be assigned to object properties when creating the object

  • The constructor can call class methods or other functions

  • The constructor can call the constructor of other classes

Example

Copy after login

Destructor

void __destruct ( void )
Copy after login
  • The destructor is in When the object is destroyed, it is called automatically and cannot be called explicitly.

  • The destructor cannot take parameters

Example:

name = "MyDestructableClass"; } function __destruct() { print "Destroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?>
Copy after login

If you want to know more related knowledge, please visitphp中文网.

The above is the detailed content of What is the difference between constructor and destructor in php. 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
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!