将值从一种方法返回到另一种方法

I don't know why this don't work at all. I maybe wrong with my understanding that is why.

here is the situation.

  1. MVC pattern
  2. form validation stuffs

Here are the codes

public function userExist($data) 
{
    $string = "SELECT student_number FROM users WHERE student_number = :user";
    $sth = $this->db->prepare($string);
    $sth->execute(array(
        ':user' => $data['user']
    ));
    return $sth->rowCount() == 0 ? true : false;
}

public function validate($data) {
    $this->userExist($data);
}

What i want is to return a string, that says "user exists", if the userExist method is false ... But this code doesn't work:

if($sth->rowCount() == 0) {
    return true;
} else {
    return "User Already Exists";
}

This is, how i call them in the controller:

if ($this->model->validate($data) == true) {
    $this->model->create($data);
    header('Location: '.URL.'users');
} else {
    echo $this->model->validate($data);
    die();
}

What do you think is the best solution?

First of all, you need to return the value of validate:

public function validate($data) {
    $this->userExist($data);
}

But there are some other problems here. You don't need to call $this->model->validate($data) twice in your controller. You could do something like:

$result = false;
$result = $this->model->validate($data);
if ( true === $result {
    $this->model->create($data);
    header('Location: '.URL.'users');
} else {
    die($result);
}