PHP构造用法

时间:2014-04-02 14:44:42

标签: php 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();

3 个答案:

答案 0 :(得分:2)

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

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

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

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

就其目的而言,它允许您将对象初始化为某种起始状态。您可以填充属性,设置默认值或不执行任何操作。它确实是特定于应用程序。

编辑(响应OP编辑)

我建议您保持对象属性受保护或私有,并使用setter / getters来访问该数据。您正在公开访问您的对象属性,这不是很糟糕,但它可能会导致意外更改您并不意味着要更改的内容。也许你应该考虑这样的事情:

<?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中生成错误

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

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