[英]Cannot set private variable through constructor php
我正在嘗試使用 _construct 方法設置帳戶 class 的私有屬性。 我嘗試使用this->
關鍵字,但它給了我同樣的錯誤。 這是我的代碼。
<?php
class Account
{
private $errorArray = array();
private $userName;
private $firstName;
private $lastName;
private $password;
private $password2;
private $role;
//$userName, $firstName, $lastName, $password, $password2, $role
function __construct()
{
if (isset($_POST['registerSubmit'])) {
$userName = $_POST['userName'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$role = $_POST['role'];
}
}
public function register()
{
checkUserName($userName, $errorArray);
checkFirstName($firstName, $errorArray);
checkLastName($lastName, $errorArray);
checkPasswords($password, $password2, $errorArray);
return $this->errorArray;
}
錯誤說
變量 firstName 已聲明但從未使用過。 但我顯然在 __construct 和下一個函數中使用它。
我試過了:
在構造函數中使用
this->
甚至在 checkUsername() 的參數中使用 thischeckUserName(this-?$userName, this->errorsArray);
對我來說奇怪的部分是
當我在
register()
function 中返回$errorArray
時,它沒有問題,但是當我嘗試以任何其他方式訪問它時,它給了我錯誤。
任何有關此問題的幫助將不勝感激。
我犯的愚蠢錯誤在於語法。 需要使用
$this->variableName
而不是this->$Variable name
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.