magic-s magic-s - 1 year ago 45
PHP Question

PHP error printing inside class

I'm trying to play with a class and not understand how it works. Some people explained how to pass variables between a function. My problem at the moment is errors. And how to extract errors from the class and print to the screen. My output is

only. How to get errors?

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

Answer Source

You can not return two time inside a function. But you can achieve what you want like below:-

public function getUserName() {
    $this->errors = "No MySQL connection.";
    return $this->username.'<br/>'.$this->errors;

Note:- this is the solution but your code have no mean. You have to do some useful stuff