简体   繁体   中英

Use return of function directly without a variable

Sorry for my english, can't find the correct words on Google to find why it acts like this. Must be very easy to solve!

I'm using PHP 7 and I want to use the return provided by a function for a query (bind it). If I call directly the function in my bind function, it returns nothing. If I call it outside, like $var = function($a) and use $var into the bind function, it works.

I don't get why I can't use directly the function? If I do this in the parameters of a function (like function getID($this->getName(1))) it works. why it doesn't here? The code:

Doesn't work

        $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();

Works (using a variable to call/store the function/return)

    $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();

The function is pretty basic

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

Example of return of the function

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

The "bind" function from my db pdo class

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

Thank you!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM