Return multiple values from a method in a class

感情迁移 提交于 2019-12-11 05:52:49

问题


I am trying to return multiple variables from a method.

This is what I have tried so far:

This code is the method in the class:

public function getUserInfo(){
$stmt = $this->dbh->prepare("SELECT user_id FROM oopforum_users WHERE username = ?");
$stmt->bindParam(1, $this->post_data['username']);
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$user_id = $row['user_id'];
$thumb = $row['thumbnail'];
}
return array($user_id, $thumb);
}

I attempt to place each variable in a list for use in the calling program:

session_start();
require_once('init.php');

$username = trim($_POST['username']);
// create a new object
$login = new Auth($_POST, $dbh);

    if($login->validateLogin()){

        $_SESSION['loggedin'] = true;
        list($user_id, $thumb) = $login->getUserInfo($user_id, $thumb);
        echo $user_id . ' ' . $thumb;

    }

This hasn't work.

How can I return an array of multiple variables from a method within a class for use in the calling program?


回答1:


The method that you define in the class doesn't match what you are calling.

// In the class, you have:
getUserInfo();

// But you call this:
getUserInfo($user_id, $thumb);

Because of this, PHP thinks you are calling a different method, and thus returns nothing (at least nothing of use here).

Your call should look like this:

list($user_id, $thumb) = $login->getUserInfo(); //Note that there are no parameters.


Another Option

Something else you should look at is using an associative array. It would look something like this:

//In the class:
public function getUserInfo() {
  ...
  return array(
    'id'    => $user_id,
    'thumb' => $thumb
  );
}

//And then for your call:
$user = $login->getUserInfo();

echo $user['id'].' '.$user['thumb'];

This would be my preference when coding something like this, as I prefer having an array for related things, as opposed to a set of independent variables. But that is all preference.




回答2:


This is similar to this question- PHP: Is it possible to return multiple values from a function?

you can also take a look here. where they explain about ways to return multiple values - http://php.net/manual/en/functions.returning-values.php

One thing that I noticed is that in this line

list($user_id, $thumb) = $login->getUserInfo($user_id, $thumb);

You are passing 2 parameters here but in the function definition part you don't have parameters -

 public function getUserInfo(){
  .....
  } 



回答3:


public function getUserInfo(){
$stmt = $this->dbh->prepare("SELECT user_id FROM oopforum_users WHERE username = ?");
$stmt->bindParam(1, $this->post_data['username']);
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    $user_id = $row['user_id'];
    $thumb = $row['thumbnail'];
    $return[] = array($user_id, $thumb);
}
return $return;
}



回答4:


You could create a class, e.g., UserInfo, specifically for holding user information and return that.

You could also return an associative array, e.g...

$userInfo = array(
    'id' => ...,
    'thumb' => ...
);

A third alternative is to use references, but in this context I recommend staying away from that.



来源:https://stackoverflow.com/questions/11319447/return-multiple-values-from-a-method-in-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!