我正在尝试玩一个类,但不明白它是如何工作的。有些人解释了如何在函数之间传递变量。我现在的问题是错误。以及如何从类中提取错误并打印到屏幕上。我的输出仅是用户名
。如何获取错误?
class form
{
protected $username;
protected $password;
protected $errors = array();
function __construct($username, $password){
$this->username = $username;
$this->password = $password;
}
public function get_errors()
{
return $this->errors;
}
public function getPassword(){
return $this->password;
}
public function getUserName() {
return $this->username;
return $this->errors = "No MySQL connection.";
}
}
$test = new form('name1', 'passw2');
echo $test->getUserName();
最佳答案
您不能在函数内返回两次。但你可以实现你想要的,如下所示:-
public function getUserName() {
$this->errors = "No MySQL connection.";
return $this->username.'<br/>'.$this->errors;
}
注意:- 这是解决方案,但您的代码没有任何意义。你必须做一些有用的事情
关于PHP 在类中打印错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38014383/