从类中的方法返回多个值

时间:2023-02-03 02:02:38

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?

如何从类中的方法返回多个变量的数组以供在调用程序中使用?

4 个解决方案

#1


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).

因此,PHP认为你正在调用一个不同的方法,因此什么都不返回(至少没有任何用处)。

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


1  

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

这类似于这个问题 - PHP:是否可以从函数返回多个值?

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

你也可以看看这里。在那里他们解释了返回多个值的方法 - 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 -

你在这里传递2个参数,但在函数定义部分你没有参数 -

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

#3


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;
}

#4


0  

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

您可以创建一个类,例如UserInfo,专门用于保存用户信息并返回该信息。

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.

第三种选择是使用引用,但在这种情况下,我建议远离它。

#1


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).

因此,PHP认为你正在调用一个不同的方法,因此什么都不返回(至少没有任何用处)。

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


1  

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

这类似于这个问题 - PHP:是否可以从函数返回多个值?

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

你也可以看看这里。在那里他们解释了返回多个值的方法 - 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 -

你在这里传递2个参数,但在函数定义部分你没有参数 -

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

#3


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;
}

#4


0  

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

您可以创建一个类,例如UserInfo,专门用于保存用户信息并返回该信息。

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.

第三种选择是使用引用,但在这种情况下,我建议远离它。