使用PHP调用类方法时出错
我在使用PHP调用一个类方法时出错。我的代码如下 user.php:使用PHP调用类方法时出错,php,class,Php,Class,我在使用PHP调用一个类方法时出错。我的代码如下 user.php: require_once ('common.php'); $userClass=new CommonUtilFuncs(); $login_code =$userClass->getToken(6); echo $login_code; common.php: class CommonUtilFuncs{ function __construct() { } // destructor
require_once ('common.php');
$userClass=new CommonUtilFuncs();
$login_code =$userClass->getToken(6);
echo $login_code;
common.php:
class CommonUtilFuncs{
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
public function getToken($length){
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
$max = strlen($codeAlphabet); // edited
$crypto=$this->crypto_rand_secure(0, $max - 1);
for ($i = 0; $i < $length; $i++) {
$token .= $codeAlphabet[$crypto];
}
return $token;
}
private 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;
}
}
在user.php页面中,我试图打印登录代码,但没有结果。页面不工作错误即将出现。从更改文件名
common.php
到
并将require_once'common.php'更改为require_once'CommonUtilFuncs.php' 您遇到了什么错误?@mi6crazyheart:当我运行该文件时,页面不工作消息正在显示。您是否遇到任何PHP错误?如果是,请同时打印错误消息。否,此消息只会出现。请打开错误报告。这是您知道即使文件名与类名不同,代码仍应工作的唯一方法。
CommonUtilFuncs.php