【发布时间】:2014-09-19 08:53:11
【问题描述】:
我正在开发一个基于第三方的程序。我编写了一个类并使用 __autoload() 来加载该类。
程序运行如下:
1.我将脚本上传到远程服务器。
2.第三方服务器将请求我的脚本,并带有一些获取参数。
3.我的脚本返回str,第三方服务器会检查str,如果str与get参数相同,则有效,否则无效。
现在,问题是:
当我使用 __autoload() 时,它会引发一些错误,当我用 include/require 替换 __autoload 时,它会准确运行。
代码如下:
wechat.class.php
public function __construct($options){
$this->_token=isset($options['token'])?$options['token']:'';
}
public function test(){
var_dump($this->_token);
}
private function checkSignature(){
$signature = isset($_GET["signature"])?$_GET["signature"]:'';
$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
$nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
$token = $this->_token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function valid($return=false){
$echostr=isset($_GET['echostr'])?$_GET['echostr']:'';
if($return){
if($echostr){
if($this->checkSignature()){
return $echostr;
}else{
return false;
}
}else{
return $this->checkSignature();
}
}else{
if($echostr){
if($this->checkSignature()){
die($echostr);
}else{
die('no access');
}
}else{
if($this->checkSignature()){
return true;
}else{
die('no access');
}
}
}
}
}
?>
wechat.php
<?php
function __autoload($classname){
include "./wechat/".$classname.".class.php";
}
$options=array(
'token'=>'tudouya',
);
$wechat=new Wechat($options);
$wechat->valid();
?>
另外,我的 php 版本是 5.4。我测试了这个脚本很长时间,发现 __autoload() 和 include 在 php 5.2 中都可以正常工作,但在 php 5.4 中不行。
我什至认为php中是否有错误,但我无法决定。
希望我能清楚地描述我的问题,并对我的英语不好感到抱歉。
【问题讨论】:
-
最好使用带有
__DIR__指令的绝对路径。 -
您的脚本应该可以正常运行...(我猜路径是正确的...)顺便说一句,我建议您使用
spl_autoload_register而不是__autoloadAutoloading Classes 。 -
@KeVin,@Debflav,我试试你说的,还是不行。我认为这不是问题点。
-
也许它不能解决你的问题。但是如果您迁移到 PHP5.4,您应该注意:“spl_autoload_register() 为自动加载类提供了更灵活的替代方案。因此,不鼓励使用 __autoload(),并且将来可能会弃用或删除。”
-
@Debflav,你是对的,谢谢,我会这样做的。