1

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?

crmepham
  • 4,676
  • 19
  • 80
  • 155

4 Answers4

3

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.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • I attempted your second option but it only returns the user id. Here is my code at the moment: http://pastebin.com/yB5dmLjL – crmepham Jul 03 '12 at 21:59
  • print_r shows the following: `Array ( [loggedin] => 1 [password] => [id] => 1 [thumb] => )` – crmepham Jul 03 '12 at 22:00
  • @cement if it isn't showing up, you should check your database and make sure the value is set there. Your code looks correct to me. – Jon Egeland Jul 04 '12 at 00:56
1

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(){
  .....
  } 
Community
  • 1
  • 1
geeky_bat
  • 318
  • 1
  • 3
  • 15
0
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;
}
William Isted
  • 11,641
  • 4
  • 30
  • 45
0

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.

kevin628
  • 3,458
  • 3
  • 30
  • 44