views:

3013

answers:

4

I have been noticing *__construct* a lot with classes. I did a little reading and surfing the web, but I couldn't find an explanation I could understand. I am just beginning with OOP.

I was wondering if someone could give me a general what it is, and then a simple example of how it is used with php?

Thanks,
Levi

+1  A: 

Its another way to declare the constructor. You can also use the class name, for ex:

class Cat
{
    function Cat()
    {
        echo 'meow';
    }
}

and

class Cat
{
    function __construct()
    {
        echo 'meow';
    }
}

Are equivalent. They are called whenever a new instance of the class is created, in this case, they will be called with this line:

$cat = new Cat();
Logan Serman
+14  A: 

The "__construct" was introduced in PHP5 and it is the right way to define your, well, constructors (in PHP4 you used the name of the class for a constructor). You are not required to define a constructor in your class, but if you wish to pass any parameters on object construction then you need one.

An example could go like this:

class Database {
  protected $userName;
  protected $password;
  protected $dbName;

  public function __construct ( $UserName, $Password, $DbName ) {
    $this->userName = $UserName;
    $this->password = $Password;
    $this->dbName = $DbName;
  }
}

// and you would use this as:
$db = new Database ( 'user_name', 'password', 'database_name' );

Everything else is explained in the PHP manual: click here

Jan Hancic
@Jan: I rolled back your rollback, because you removed a whole bunch of useful information. Sorry if my edits offended you in some way, but the point of the site is to allow people to edit answers to make them "as good as we can get".
Rob
Rob I don't know what you are trying to accomplish, but there is a "YourAnswer" box bellow where you can post your own (as opposed to deleting my response and inserting your text in place of mine) answer.
Jan Hancic
Then add the missing information. Don't just remove my whole response :) That doesn't make sense to me :)
Jan Hancic
(sigh) I've extracted my version to my "own" answer. Clearly, the "wiki" aspect of SO is broken.
Rob
I am all for improving answers. Hey, that's why we are here for. But deleting everything and inserting something new is not improving, that's rewriting ...
Jan Hancic
+4  A: 

__construct() is the method name for the constructor. The constructor is called on an object after it has been created, and is a good place to put initialisation code, etc.

class Person {

    public function __construct() {
        // Code called for each new Person we create
    }

}

$person = new Person();

A constructor can accept parameters in the normal manner, which are passed when the object is created, e.g.

class Person {

    public $name = '';

    public function __construct( $name ) {
        $this->name = $name;
    }

}

$person = new Person( "Joe" );
echo $person->name;

Unlike some other languages (e.g. Java), PHP doesn't support overloading the constructor (that is, having multiple constructors which accept different parameters). You can achieve this effect using static methods.

Note: I retrieved this from the log of the (at time of this writing) accepted answer, since the original author has issues with having their answer improved.

Rob
+1  A: 

So assuming I already have the class:

class Cat
{
    function Cat()
    {
        echo 'meow';
    }
}

What are the benefits of replacing "function Cat()" with "function __construct()" ?

Matt
Look at the last example here: http://www.php.net/manual/en/language.oop.constructor.phpThis can't happen in PHP5 using the "__construct"Other than that I can't really think of any benefits, but the "_construct" is the preferred way, and ClassName() may very well be unsupported in future versions
Jan Hancic
Ahh, alright. Thanks.
Matt