0

I'm learning how to use classes properly... I'm looking at usercake and most of it makes sense, however I'm not sure what the __construct function is doing. I understand it gets called when you create the class... i.e. $loggedInUser = new loggedInUser();

What does the stuff below do and why do I need it?

    function __construct($user, $display, $title, $pass, $email)
{
    //Used for display only
    $this->displayname = $display;

    //Sanitize
    $this->clean_email = sanitize($email);
    $this->clean_password = trim($pass);
    $this->username = sanitize($user);
    $this->title = sanitize($title);

    if(usernameExists($this->username))
    {
        $this->username_taken = true;
    }
    else if(displayNameExists($this->displayname))
    {
        $this->displayname_taken = true;
    }
    else if(emailExists($this->clean_email))
    {
        $this->email_taken = true;
    }
    else
    {
        //No problems have been found.
        $this->status = true;
    }
}

Edit: Here is how the class gets called:

                    $loggedInUser = new loggedInUser();
                $loggedInUser->email = $userdetails["email"];
                $loggedInUser->user_id = $userdetails["id"];
                $loggedInUser->hash_pw = $userdetails["password"];
                $loggedInUser->title = $userdetails["title"];
                $loggedInUser->displayname = $userdetails["display_name"];
                $loggedInUser->username = $userdetails["user_name"];
                $loggedInUser->alerts = array();
Nikki
  • 79
  • 1
  • 8
  • 1
    `__construct` is the constructor. prior to php5 the constructor's name was equal to the class name, like in c# and java. the constructor's purpose is to initialize the state of the newly created object. – Joshua Apr 02 '14 at 14:48
  • 1
    Means your question `Why do I need __construct` or `Why do I need classes`? – vp_arth Apr 02 '14 at 14:50
  • Why do I need __construct. What does initializing do? If I do not initialize, what happens? – Nikki Apr 02 '14 at 14:55

3 Answers3

2

It is the constructor function. When you create an instance of that class your constructor function is run.

For example, with your constructor (I don't know your class name).

$class = new MyClass("jdoe", "John Doe", "Standard User", "Passw0rd!","jdoe@example.com");`

This will create a new MyClass and store it in $class.

As for its purpose, it lets you initialize the object to a starting state of some kind. You can populate properties, set default values, or just do nothing. It is really application specific.

EDIT (in response to OP's edit)

I would really suggest keeping your object properties either protected or private and use setter/getters to access that data. You are giving public access to your objects properties, which isn't bad, but it can lead to accidentally changing something you didn't mean to change. Maybe you should consider something like this:

<?php

class LoggedInUser
{

    private $id;
    private $username;
    private $password;
    private $displayname;
    private $email;
    private $title;

    public function __construct($id, $username, $password, $displayname, $email, $title)
    {
        $this->setID($id);
        $this->setUsername($username);
        $this->setPassword($password);
        $this->setDisplayName($displayname);
        $this->setEmail($email);
        $this->title($title);
    }

    public function sanitize($var)
    {
        //Sanitize $var and then...
        return $var;
    }

    public function setID($num)
    {
        $this->id = $this->sanitize($num);
    }

    public function setUsername($string)
    {
        $this->username = $this->sanitize($string);
    }

    //Keep adding other "set" methods.
}

?>

Then to use this you would do something like:

$loggedin = new LoggedInUser( "arg1", "arg2", "etc..." );

Now your object is setup with the starting state. If you need to change a property later you can always do:

$loggedin->setTitle("Correct Title");

Make sure you create functions to return your properties as well. In the example above your properties are private so a call to $loggedin->title would generate an error in PHP

Crackertastic
  • 4,958
  • 2
  • 30
  • 37
  • See my new edit, the data gets added to the class after __construct runs, not when it is created. I can see if you did what you did above, you would use the __construct to 'sanatize' the data, or if there is a blank value to assign it a value if needed. Is that right? – Nikki Apr 02 '14 at 14:59
  • Never mind, I was mixing up two classes. Thanks this helped me understand the construct much better! – Nikki Apr 02 '14 at 15:06
  • @Nikki No problem. I was in an edit when you left that last comment. I added a few other notes and an example that might help you out> – Crackertastic Apr 02 '14 at 15:13
1
// Set construct function which will run when your class is called 
function __construct($user, $display, $title, $pass, $email)
{
    // Sets display name
    $this->displayname = $display;

    // Sanitizing user inputted data (See SQL injection/XSS attacks)
    $this->clean_email = sanitize($email);
    $this->clean_password = trim($pass);
    $this->username = sanitize($user);
    $this->title = sanitize($title);

    // Check if any duplicates of the user inputted data exist already in the database
    // If any of these checks return true, the status wont be set to true, and further code wont be ran
    if(usernameExists($this->username))
    {
    $this->username_taken = true;
    }
    else if(displayNameExists($this->displayname))
    {
    $this->displayname_taken = true;
    }
    else if(emailExists($this->clean_email))
    {
    $this->email_taken = true;
    }
    else
    {
    // No duplicate information has been found, set status and continue registration
    $this->status = true;
    }
}
d.abyss
  • 204
  • 1
  • 4
  • 26
  • Thank you...One more question, please see my edit above... You can see it doesn't have any data to check when the construct runs? – Nikki Apr 02 '14 at 15:03
0

You need it because initialize the object you create.

Alex7
  • 560
  • 3
  • 19