Results 1 to 5 of 5

Thread: Constructors and Destructors in PHP

  1. #1
    Join Date
    May 2008
    Posts
    248

    Constructors and Destructors in PHP

    Hello sir,

    I need to know about the constructor and destructor in the PHP programming as I have done very little practice in C++.Is something different from the C++ or similar.I will be highly obliged to you for your help.

    Is there any concept regarding overloading of these components.

    Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Constructors in PHP

    Constructors in PHP

    PHP programming allows to the programmer to declare constructor methods for the existing classes.The constructor takes the following way to define -

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

    An example of constructor-

    Code:
    <?php
    class Main_Class {
       function __construct() {
           print "In Main_Class constructor\n";
       }
    }
    
    class Sub_Class extends Main_Class {
       function __construct() {
           parent::__construct();
           print "In Sub_Class constructor\n";
       }
    }
    
    $New_obj = new Main_Class();
    $New_obj = new Sub_Class();
    ?>

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Destructors in PHP programming

    Destructors

    PHP programming allows us to use and destructor concept which is very similar to other concept which is in object-oriented languages,like C++.It is primarily used to remove the declared constructor in the program.

    It takes the following syntax to declare -

    void __destruct ( void )

    Similar to constructors,the parent destructors didn't be called implicitly by the engine.To execute a parent destructor, at-least one should explicitly called to parent::__destruct() in the destructor section.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Example of Destructors in PHP

    You can declare the destructor in the program which is used to remove the declared constructor in the program.

    An example of destructor would be as follows-

    Code:
    <?php
    class Destroy_Class {
       function __construct() {
           print "In constructor\n";
           $this->name = "Destroy_Class";
       }
    
       function __destruct() {
           print "Destroying " . $this->name . "\n";
       }
    }
    
    $New_obj = new Destroy_Class();
    ?>

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Abstract class with Constructors and Destructors

    Abstract class with Constructors and Destructors

    I am going to show you an example which uses the abstract class and can be adjusted with the code of pHP which would be as follows :

    Code:
    <?php
    
    abstract class animal
    {
            abstract function gt_owned();
            private $age;
    
            protected function __construct($age) {
                    $this->age = $age;
                    }
    
            public function gt_age()
                    {
                    return $this->$age;
                    }
    }
    ?>
    
    <body><h1>Abstract class code</h1>
    
    <?php
    
    $charlie = new animal(6);
    $catage = $charlie -> getage();
    print "Charlie is $catage years old<br>";
    ?>
    <hr>
    </body>

Similar Threads

  1. Constructors in Java help
    By cloud101 in forum Software Development
    Replies: 4
    Last Post: 18-01-2012, 05:02 PM
  2. Do Virtual Destructors exist in C++ ?
    By Satchel in forum Operating Systems
    Replies: 6
    Last Post: 20-01-2011, 10:25 PM
  3. what are constructors in C++?
    By Asis in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 08:38 AM
  4. Does anyone know about virtual constructors and destructors?
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 01:57 PM
  5. What are the Constructors and Destructors?
    By RupaliP in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 07:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,662,756.06973 seconds with 17 queries