You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中UPLOAD类构造函数已废弃报错问题排查求助

PHP UPLOAD类构造函数过时错误修复方案

错误原因

你遇到的是PHP版本兼容性问题:在PHP 7.0及后续版本中,与类同名的方法不再被识别为构造函数,这种写法已被废弃,会触发警告,未来版本会彻底失效。你的UPLOAD类中function UPLOAD($URI)就是这种过时的构造函数写法。

修复方案

方案1:使用标准__construct()方法(推荐,符合现代PHP规范)

直接将原同名构造函数替换为PHP官方标准的构造函数__construct(),修改后的核心代码如下:

class UPLOAD
{
    var $URI='';
    var $Host='localhost';
    var $Port='80';
    var $Fields=array();
    var $Files=array();
    var $Headers=array();
    var $Request='';
    var $RequestHeaders='';
    var $RequestBody='';
    var $Response='';
    var $ResponseHeaders='';
    var $ResponseBody='';
    var $log='';
    var $errors=array();
    var $UseProxy=false;
    var $ProxyHost='localhost';
    var $ProxyPort=3128;
    
    // 替换为标准构造函数
    function __construct($URI)
    {
        $this->log .= 'Creating object with URI: '.htmlspecialchars($URI)."\n";
        if (preg_match("/^https:\/\/([^\/:]+)[:]?(\d*)?(.*)/i", $URI, $m))
        {
            $this->URI = $URI;
            $this->Host = $m[1];
            $this->Port = ((int)$m[2])==0 ? 80 : (int)$m[2];
            $this->log .= 'Object with URI '.htmlspecialchars($URI)." created.\n";
            $this->log .= '  host: '.htmlspecialchars($this->Host)."\n";
            $this->log .= '  port: '.htmlspecialchars($this->Port)."\n";
        }
        else
        {
            $this->log .= 'Object with URI '.htmlspecialchars($URI)." create failed.\n";
            $this->errors[] = 'Access URI '.htmlspecialchars('"'.$URI.'"').' is not valid';
        }
    }

    // 其余方法保持不变
    function SetFields($field, $value='') { /* ... */ }
    function SetFiles($field, $filename='', $content='') { /* ... */ }
    function Send() { /* ... */ }
    function calculate() { /* ... */ }
}

方案2:兼容新旧PHP版本

如果需要同时支持PHP 5.x和7.x+,可以保留两种构造函数写法,让旧版调用自动指向新版构造函数:

class UPLOAD
{
    // 类属性保持不变
    
    // 旧版构造函数(兼容PHP 5.x)
    function UPLOAD($URI)
    {
        $this->__construct($URI);
    }
    
    // 新版标准构造函数(兼容PHP 7+)
    function __construct($URI)
    {
        // 原构造函数逻辑完全保留
        $this->log .= 'Creating object with URI: '.htmlspecialchars($URI)."\n";
        if (preg_match("/^https:\/\/([^\/:]+)[:]?(\d*)?(.*)/i", $URI, $m))
        {
            $this->URI = $URI;
            $this->Host = $m[1];
            $this->Port = ((int)$m[2])==0 ? 80 : (int)$m[2];
            $this->log .= 'Object with URI '.htmlspecialchars($URI)." created.\n";
            $this->log .= '  host: '.htmlspecialchars($this->Host)."\n";
            $this->log .= '  port: '.htmlspecialchars($this->Port)."\n";
        }
        else
        {
            $this->log .= 'Object with URI '.htmlspecialchars($URI)." create failed.\n";
            $this->errors[] = 'Access URI '.htmlspecialchars('"'.$URI.'"').' is not valid';
        }
    }

    // 其余方法保持不变
}

额外优化建议

  • 原代码calculate()方法中请求头写的是HTTPS/1.0,属于笔误,正确写法应为HTTP/1.0或HTTP/1.1,建议修正:
    $headers = 'POST '.$this->URI." HTTP/1.0\r\n";
    
  • 类属性使用var是PHP旧语法,现代PHP建议用public/private/protected修饰,比如public $URI='';,提升代码规范性。

内容的提问来源于stack exchange,提问作者DCJones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 08:56:10