yii2 - PHP 中的 new static($user) 有何作用,如何理解

來源:互聯網
上載者:User
如何理解PHP中的 new static() 的用法
在yii2的使用者登入處理模型 /model/User.php 看到了這一段:

php [            'id' => '100',            'username' => 'admin',            'password' => 'admin',            'authKey' => 'test100key',            'accessToken' => '100-token',        ],        '101' => [            'id' => '101',            'username' => 'demo',            'password' => 'demo',            'authKey' => 'test101key',            'accessToken' => '101-token',        ],    ];    /**     * @inheritdoc     */    public static function findIdentity($id)    {        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;    }    /**     * @inheritdoc     */    public static function findIdentityByAccessToken($token, $type = null)    {        foreach (self::$users as $user) {            if ($user['accessToken'] === $token) {                return new static($user);            }        }        return null;    }    /**     * Finds user by username     *     * @param  string      $username     * @return static|null     */    public static function findByUsername($username)    {        foreach (self::$users as $user) {            if (strcasecmp($user['username'], $username) === 0) {                return new static($user);            }        }        return null;    }    /**     * @inheritdoc     */    public function getId()    {        return $this->id;    }    /**     * @inheritdoc     */    public function getAuthKey()    {        return $this->authKey;    }    /**     * @inheritdoc     */    public function validateAuthKey($authKey)    {        return $this->authKey === $authKey;    }    /**     * Validates password     *     * @param  string  $password password to validate     * @return boolean if password provided is valid for current user     */    public function validatePassword($password)    {        return $this->password === $password;    }}

return $user ? new static($user) : null,如果 self::$sers[$id] 裡面沒有 User 類定義的這些屬性,返回就會報錯,為什麼會這樣。如何理解

回複內容:

如何理解PHP中的 new static() 的用法
在yii2的使用者登入處理模型/model/User.php 看到了這一段:

php [            'id' => '100',            'username' => 'admin',            'password' => 'admin',            'authKey' => 'test100key',            'accessToken' => '100-token',        ],        '101' => [            'id' => '101',            'username' => 'demo',            'password' => 'demo',            'authKey' => 'test101key',            'accessToken' => '101-token',        ],    ];    /**     * @inheritdoc     */    public static function findIdentity($id)    {        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;    }    /**     * @inheritdoc     */    public static function findIdentityByAccessToken($token, $type = null)    {        foreach (self::$users as $user) {            if ($user['accessToken'] === $token) {                return new static($user);            }        }        return null;    }    /**     * Finds user by username     *     * @param  string      $username     * @return static|null     */    public static function findByUsername($username)    {        foreach (self::$users as $user) {            if (strcasecmp($user['username'], $username) === 0) {                return new static($user);            }        }        return null;    }    /**     * @inheritdoc     */    public function getId()    {        return $this->id;    }    /**     * @inheritdoc     */    public function getAuthKey()    {        return $this->authKey;    }    /**     * @inheritdoc     */    public function validateAuthKey($authKey)    {        return $this->authKey === $authKey;    }    /**     * Validates password     *     * @param  string  $password password to validate     * @return boolean if password provided is valid for current user     */    public function validatePassword($password)    {        return $this->password === $password;    }}

return $user ? new static($user) : null,如果 self::$sers[$id] 裡面沒有 User 類定義的這些屬性,返回就會報錯,為什麼會這樣。如何理解

http://stackoverflow.com/questions/5197300/new-self-vs-new-static

這裡有說明

new static : 建立入口對象的新對象。
new self : 建立擁有此方法對象的新對象。
new parent : 建立擁有此方法對象父類的新對象。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.