如何从特征中的一个函数调用函数(父特征中存在两个函数)

How to call function from one function in traits (two function are present in parent traits)

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

---------------------------error-------------------------------- Fatal error: Call to undefined function crypto_rand_secure() in /var/www/html/clients/assuredo/include/config/generateToken/token.php on line 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;
        }
}

像往常一样在所有类中,你必须在函数名前使用$this->