How to use constructor in php?

Discussion in 'PHP' started by leadgeneration, Jan 18, 2011.

  1. #1
    Hello every one
    I am facing problem in constructor .Even i know about this concept but using this key in calling function construction .
    Please suggest some coding example .
     
    leadgeneration, Jan 18, 2011 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    What problem are you having exactly?

    <?php
    class mytest
      {
      # The constructor ....
      function __construct()
        {
        $this->test = "hello";
        }
    
      public function output()
        {
        print $this->test . "\n";
        }
      }
    
    $test = new mytest;
    $test->output();
    ?>
    PHP:
    You might also want to read up on constructors and destructors on the PHP site;
    http://php.net/manual/en/language.oop5.decon.php
     
    lukeg32, Jan 18, 2011 IP
  3. rusianace

    rusianace Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    do you mean calling the constructor function? it is automatically called after instantiating an object
     
    rusianace, Jan 18, 2011 IP
  4. Zetiz

    Zetiz Active Member

    Messages:
    668
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    As he said, have a look at the use of constructors in the official site. It will help you.

     
    Zetiz, Jan 18, 2011 IP