PHP Constructors and destructors

Source: Internet
Author: User

Refer to "PHP Object-oriented-constructors, destructors," and "official documents"

constructor Function
Mixed $args [, $... ]] )
    • Constructors can accept parameters and be able to assign values to object properties when creating an object
    • Constructors can call class methods or other functions
    • Constructors can call constructors of other classes

PHP 5 allows the developer to define a method as a constructor in a class. Classes with constructors call this method each time an 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 for 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.

Examples of constructor use:

<?PHPclassBaseClass {function__construct () {Print"In BaseClass constructor\n"; }}classSubclassextendsBaseClass {function__construct () {Parent::__construct (); Print"In Subclass constructor\n"; }}$obj=NewBaseClass ();$obj=Newsubclass ();?>

For backwards compatibility, if PHP 5 cannot find the __construct () function in the class, it tries to find the old-fashioned constructor, which is the function with the same name as the class. So the only thing that can create a compatibility problem is that there is already a method named __construct () in the class, but it is not a constructor.

Destructors
void __destruct (void)
    • Destructors are calls that are automatically called when an object is destroyed and cannot be displayed.
    • Destructors cannot take parameters.

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.

Note: The destructor is called when the script is closed, at which point all header information is sent. Attempting to throw an exception in a destructor can result in a fatal error. 

Examples of destructor use:

 <? php  class   Mydestructableclass { function   __construct () { print  "in constructor\n" ;     $this ->name = "Mydestructableclass" ;  function   __destruct () { print  "destroying".  $this ->name.   "\ n" ; }}   $obj  = new   Mydestructableclass (); ? 

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

Destructors may be called (but not necessarily) in the following situations:

    • After the PHP page has finished loading;
    • Unset () class;
    • When a variable reference points to another object or value;

PHP's memory recovery mechanism is similar to that of Java, where no reference objects are destroyed and recycled, using a reference counter technique.

Example:

 <? php  class   test{ function   __destruct () { echo  "is called when the object is destroyed!!!    "; }}   $a  =  $b  =  $c  = new   Test ();   $a  = null  ;  unset  ( $b   echo  "; ? 

In this case, for example, there are three variables that reference $ A, $b, $c point to the test object, the test object has 3 reference counts, and when $ A = NULL, $a the reference to the test object is lost, the count-1, becomes 2, and when $b is unset (), $b reference to the test object is lost, The count is 1, becomes 1, the last page is loaded, the reference to the test object is automatically freed, the count is then 1, and the 0,test object is destroyed without a variable reference, and the destructor is called.

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.