【问题标题】:__autoload() vs include,will __autoload() output something?__autoload() vs include,__autoload() 会输出一些东西吗?
【发布时间】: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 而不是 __autoload Autoloading Classes
  • @KeVin,@Debflav,我试试你说的,还是不行。我认为这不是问题点。
  • 也许它不能解决你的问题。但是如果您迁移到 PHP5.4,您应该注意:“spl_autoload_register() 为自动加载类提供了更灵活的替代方案。因此,不鼓励使用 __autoload(),并且将来可能会弃用或删除。”
  • @Debflav,你是对的,谢谢,我会这样做的。

标签: php include autoload


【解决方案1】:

答案是:

include "./wechat/".strtolower($classname).".class.php";

否则它将尝试包含Wechat.class.php(带有大写w,它不作为文件存在。

【讨论】:

  • 谢谢,我再试一次你说的,它工作。但另一个问题提出。正如你所说,(用大写的 w,它不作为文件存在),如果这样,为什么我可以新建一个对象并且程序不会引发错误?
  • 可能您的error_reporting 没有设置为E_ALL,因为尝试实例化未知类会引发致命错误。
  • @Debflav,首先我的error_reporting设置为E_ALL。另外,我可以实例化类并调用成员函数。甚至,成员函数可以正常运行!跨度>
  • @tudouya 所以,对不起,我误解了你真正的问题。
  • classname是Wechat(大写w),而FILEname是class.wechat.php(小写w)。所以 include('class.wechat.php'); (小写 w)在你必须使用 new Wechat() 时有效; (大写 W)创建类的实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 2014-09-28
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多