簡體   English   中英

PHP構造用法

[英]PHP Construct Usage

我正在學習如何正確使用類……我正在研究usercake,並且大多數情況下都是有意義的,但是我不確定__construct函數在做什么。 我了解您在創建類時會調用它...即$ loggedInUser = new loggingInUser();

以下內容是做什么的,為什么我需要它?

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

它是構造函數。 創建該類的實例時,將運行構造函數。

例如,使用您的構造函數(我不知道您的類名)。

$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中生成錯誤

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

您需要它,因為初始化您創建的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM