PHP Constructors and destructors

Source: Internet
Author: User

constructor function

void __construct ([ mixed $args [, $... ]])

PHP 5 allows the developer to define a method as a constructor in a class. Classes with constructors Call this method each time a new object is created, making it ideal for doing some initialization work before using the object.

If a constructor is defined in a subclass, the constructor of its parent class is not implicitly called. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the child class . If a subclass does not have a constructor defined, it inherits from the parent class like a normal class method (if it is not defined as private).

Destructors

void __destruct ( void)

PHP 5 introduces the concept of destructors, similar to other object-oriented languages such as C + +. Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.

As with constructors, the destructor of the parent class is not called by the engine. To execute a destructor for a parent class, you must explicitly call Parent::__destruct () in the destructor body of the subclass . Also, as with constructors, subclasses inherit the parent class if they do not have a destructor defined by themselves.

Destructors are called even when the script is run with exit (). Calling exit () in the destructor will abort the operation of the rest of the shutdown operation.

Throwing an exception in a destructor (called when the script terminates) causes a fatal error.

PHP Constructors and destructors

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.