Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

jason chan
jason chan
31,009 Points

what does __construct mean?

class Person {

    public $name;

    public $age;

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

$john = new Person('jack tran');
$john->age = 30;

$john->age++;

var_dump($john);

I don't know what construct means. trying to learn oop for php

3 Answers

Konrad Pilch
Konrad Pilch
2,435 Points

HI,

This is actually pretty easy, it would be easier if there was more into this code as it would make it cleaner.

Im not the best in eexplaining this but, ill try my best.

Lets add public $surname to it, when you add this, u can add it to the construct so its $name, $surname., and $age.

Now, when u create a new person , you can write like ( the name ) , ( surname ) , (age) .

So now every new person u type , it will have these , like u have now.

Something like that xd i know how to use it and when u just dont know really how to explain this, if u go on yt , u can find amazing tutorials as well. Try PHP Academy , they will have it.

Hope this helps .

jason chan
jason chan
31,009 Points

I think it's used to construct other instances of class and object.

Yes i watched that guy like a long time ago. But never actually coded anything more for like a fun thing.

Konrad Pilch
Konrad Pilch
2,435 Points

I dont know how to explain it well, nor im expert in this, since im fairly new to this as well : p

jason chan
jason chan
31,009 Points

I'm trying to get the hang of php. I'm still better than vanilla javascript, and jquery. hell im even better at python. But I like how the server is already made for apache. I don't wanna to code my own server script on node. LOLs. LAZY.