PHP类使用 - 未定义的变量错误

时间:2014-08-31 22:11:31

标签: php class parameters

两个文件: - Login.php - 注册。

Login.php有两个变量。 Registration.php有三个。

Login.php只有两个变量,但是该类使用了三个变量,并在调用方法时返回错误,该变量未定义。

如果上述说明不明白,请阅读代码中的注释。

为registration.php

 include("validate_class.php");
    //Fields in registration
    $username                    = "";
    $password                    = "brokenship";
    $email                        = "sami@hotmail.com";

    //Instantiating my class.
    $instantiateClass            = new validate_class($username, $password, $email);

    //This is called in registration.php file. This validates the three fields registration.php has. $email, $password, 
    $instantiateClass->validate_empty(); 

的login.php

include("validate_class.php");
//Fields in login
$username                   = "";
$password                   = "brokenship";

//Instantiating my class.
$instantiateClass           = new validate_class($username, $password, $email);

//This is called in registration.php file. This validates the three fields registration.php has. $email, $password, 
$instantiateClass->validate_empty();

//*****Now, here's the problem. I wanna only give two parameters..*****//

validate_class.php

class validate_class {
    function __construct($username, $password, $email){

        //Regular value
        $this->username                         = $username;
        $this->password                         = $password;
        $this->email                            = $email;

        //Add empty function
        $this->empty_username                   = empty($username);
        $this->empty_password                   = empty($password);
        $this->empty_email                      = empty($email);
    }

    function validate_empty(){
        if( ($this->empty_username) or ($this->empty_password) or ($this->empty_email) ){
            echo "Fill all the fields";
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

如果没有提供电子邮件,则显示提示:

class validate_class {
function __construct($username, $password, $email=false){

    //Regular value
    $this->username                         = $username;
    $this->password                         = $password;
    $this->email                            = $email;

    //Add empty function
    $this->empty_username                   = empty($username);
    $this->empty_password                   = empty($password);
    $this->empty_email                      = $email != false && empty($email);
}

function validate_empty(){
    if( ($this->empty_username) or ($this->empty_password) or ($this->empty_email) ){
        echo "Fill all the fields";
    }
}   
}