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

kin*_*ute 2 php pdo bind return function

对不起,我的英文,在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();
Run Code Online (Sandbox Code Playgroud)

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

    $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();
Run Code Online (Sandbox Code Playgroud)

这个功能非常基础

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

返回函数的示例

var_dump($this->getTodayWinnerFs(494));
// string(2) "13"
Run Code Online (Sandbox Code Playgroud)

我的db pdo类中的"bind"函数

        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);
        }
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 5

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