0

我正在学习如何正确使用类...我正在查看 usercake ,其中大部分是有道理的,但是我不确定 __construct 函数在做什么。我知道当你创建类时它会被调用......即 $loggedInUser = new loggedInUser();

下面的东西有什么作用,我为什么需要它?

    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;
    }
}

编辑:这是类的调用方式:

                    $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();
4

3 回答 3

2

它是构造函数。当您创建该类的实例时,您的构造函数将运行。

例如,使用您的构造函数(我不知道您的类名)。

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

这将创建一个新的MyClass并将其存储在$class.

至于其目的,它允许您将对象初始化为某种起始状态。您可以填充属性、设置默认值或什么都不做。它确实是特定于应用程序的。

编辑(响应 OP 的编辑)

我真的建议将您的对象属性保持为受保护或私有,并使用 setter/getter 来访问该数据。您正在公开访问您的对象属性,这还不错,但它可能会导致意外更改您不想更改的内容。也许你应该考虑这样的事情:

<?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.
}

?>

然后要使用它,您将执行以下操作:

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

现在您的对象已设置为初始状态。如果您以后需要更改属性,您可以随时执行以下操作:

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

确保您也创建了返回属性的函数。在上面的示例中,您的属性是私有的,因此调用$loggedin->title会在 PHP 中生成错误

于 2014-04-02T14:50:31.397 回答
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;
    }
}
于 2014-04-02T14:54:00.563 回答
0

您需要它,因为初始化您创建的对象。

于 2014-04-02T14:48:25.030 回答