【问题标题】:How to call function from one function in traits (two function are present in parent traits)如何从特征中的一个函数调用函数(父特征中存在两个函数)
【发布时间】:2019-09-14 13:36:00
【问题描述】:

我在 php.ini 中定义了特征。我试图在两个具有相同特征的函数之间进行通信,但我遇到了致命错误。

---------------错误------- ------------- 致命错误:调用未定义函数 crypto_rand_secure() 在 /var/www/html/clients/assuredo/include/config/generateToken/token.php 第 28 行

我试图在函数getToken() 中调用函数crypto_rand_secure()

trait token
    {
        public function crypto_rand_secure($min, $max)
            {
                $range = $max - $min;
                if ($range < 1) return $min; // not so random...
                $log = ceil(log($range, 2));
                $bytes = (int) ($log / 8) + 1; // length in bytes
                $bits = (int) $log + 1; // length in bits
                $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
                do {
                    $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
                    $rnd = $rnd & $filter; // discard irrelevant bits
                } while ($rnd > $range);
                return $min + $rnd;
            }

    public function getToken($length)
        {
            $token = "";
            $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
            $codeAlphabet.= "0123456789";
            $max = strlen($codeAlphabet); // edited

            for ($i=0; $i < $length; $i++) {
                $token .= $codeAlphabet[crypto_rand_secure(0, $max-1)];
            }

            return $token;
        }
}

【问题讨论】:

  • 请不要使用不必要的标签——整个问题与“函数式编程”无关,毕竟标签call不应该使用

标签: php function functional-programming call traits


【解决方案1】:

和所有类一样,你必须在函数名前使用$this-&gt;

【讨论】:

  • $token .= $codeAlphabet[$this->crypto_rand_secure(0, $max-1)];
  • 是的,完全正确-应该可以。如果没有,您应该分享更多详细信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 2013-03-12
  • 2016-02-12
相关资源
最近更新 更多