dongxiz5342 2014-09-23 06:13 采纳率: 100%
浏览 25
已采纳

从类方法返回数组

Currently I am having a few issues returning an array from a class' method. here is my little test:

Here is my method:

private function getUser($uid,$set,$params){
        switch($set){
            case 'dvar':
                $col = $params[0];
                $needle = $params[1];

                $res = $this->dbh->prepare("SELECT col,val FROM acl_users_dvars WHERE uid=:uid");
                $res->bindParam(':uid', $uid);
                $res->execute();
                foreach($res->fetchAll(PDO::FETCH_ASSOC) as $k=>$v){
                    $rows[$k]=$v;
                }
                return($rows);
            break;
        }

    }

now, vardumping directly above return $rows; results in:

Array
(
    [0] => Array
        (
            [col] => thisiscol!
            [val] => this...is.my.VARRRRRRRRRRRRRRRRR
        )

    [1] => Array
        (
            [col] => thisiscol2!
            [val] => this is my bar
        )

)

However, on the page I initialize the class and call the method:

$user = array(
    '5',
    'dvar',
    array('thisiscol!')
);
$tOb = $test->getResponce('get_user',$user);
var_dump($tOb);

I am simply returned with:

NULL

edit

as stated from the last answer, and i am sorry I completely left this out: Here is a partial from my getResponce method:

public function getResponce($do_what, $parameters){
        switch(strtolower($do_what)){
                case 'create_user':
                    self::createUser($parameters[0],$parameters[1]);
                break;
                case 'remove_user':
                    self::remUser($parameters[0]);
                break;
                case 'set_user':
                    self::setUser($parameters[0],$parameters[1],$parameters[2]);
                break;
                case 'get_user':
                    self::getUser($parameters[0],$parameters[1],$parameters[2]);
                break;
  • 写回答

4条回答 默认 最新

  • doudianhuo1129 2014-09-23 06:16
    关注

    Your function name says getUser() but you are attempting to call getResponce() -

    $user = array(
        '5',
        'dvar',
        array('thisiscol!')
    );
    $tOb = $test->getResponce('get_user',$user);
    var_dump($tOb);
    

    I think you got the answer :)

    UPDATE In your calling params array, you are just keeping array('thisiscol!')

    $user = array(
        '5',
        'dvar',
        array('thisiscol!')
    );
    

    But inside the getUser() function, you are trying to access

                $col = $params[0];
                $needle = $params[1];
    

    I see $params[1] is missing.

    ALSO

    Try adding return in your getResponce()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程