在没有变量的情况下直接使用函数返回

时间:2016-11-30 15:03:21

标签: php function pdo return bind

对不起我的英文,在Google上找不到正确的字词,以找出为什么会这样。一定很容易解决!

我正在使用PHP 7,我想使用函数提供的返回查询(绑定它)。如果我直接调用我的绑定函数中的函数,它什么都不返回。如果我在外面调用它,比如$ var = function($ a)并在绑定函数中使用$ var,它就可以工作。

我不明白为什么我不能直接使用这个功能?如果我在函数的参数中执行此操作(如函数getID($ this-> getName(1)))它可以工作。为什么不在这里?代码:

不起作用

        $this->bdd->query("UPDATE stats_of_the_days
                            SET winner_firstsolves = :winner_fs
                            WHERE id = :stat_id");

        $this->bdd->bind("winner_fs", $this->getTodayWinnerFs($winnerID), PDO::PARAM_INT);
        $this->bdd->bind("stat_id", $statID, PDO::PARAM_INT);

        $this->bdd->execute();

工作(使用变量来调用/存储函数/返回)

    $var = $this->getTodayWinnerFs($winnerID);

    $this->bdd->query("UPDATE stats_of_the_days
                        SET winner_firstsolves = :winner_fs
                        WHERE id = :stat_id");

    $this->bdd->bind("winner_fs", $var, PDO::PARAM_INT);
    $this->bdd->bind("stat_id", $statID, PDO::PARAM_INT);


    $this->bdd->execute();

该功能非常基本

public function getTodayWinnerFs($userID)
    {
      // query stuff
      return $this->bdd->resultObj()->Wins;
    }

返回功能的示例

var_dump($this->getTodayWinnerFs(494));
// string(2) "13"

我的db pdo类的“绑定”功能

        public function bind($param, $value, $type = null)
        {
            if (is_null($type)) {
                switch (true) {
                    case is_int($value):
                        $type = PDO::PARAM_INT;
                        break;
                    case is_bool($value):
                        $type = PDO::PARAM_BOOL;
                        break;
                    case is_null($value):
                        $type = PDO::PARAM_NULL;
                        break;
                    default:
                        $type = PDO::PARAM_STR;
                }
            }
            $this->stmt->bindValue($param, $value, $type);
        }

谢谢!

1 个答案:

答案 0 :(得分:5)

是因为方法getTodayWinnerFs()和主要方法正在共享$this->bdd并且在错误的调用中它处于不同的状态,我认为你应该坚持工作示例并且不要使用函数在函数调用中调用它会使它更难阅读。