PHP constructor and destructor usage examples _php tips

Source: Internet
Author: User

This article describes the PHP constructor and destructor usage. Share to everyone for your reference, specific as follows:

When a new object is instantiated, both the construction method and the destructor are invoked automatically, and the corresponding method of the parent class is used if there is inheritance.

The destructor is invoked in three different cases:

① uses unset () to destroy an object that is not invoked if there is an object-passing value;

② change the value of the variable to point to the object;

After the ③php program code runs.

<?php
class base{public
  $name;
  function __construct ($name) {
    $this->name = $name;
    echo ' obj '. $this->name. ' Have built '. ' </br> '. ' </br> ';
  }
  function __destruct () {
    echo ' obj '. $this->name. ' Have destroyed '. ' </br> '. ' </br> ';
  }
}
$a = new base (' a ');
$b = new Base (' B ');
$c = new Base (' C ');
Unset ($b);
$c = ' d ';

The results of the operation are as follows:

Obj A have built
obj b have built
obj c have built
obj B have destroyed
obj C have destroyed
obj A have destroyed

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.