繁体   English   中英

如何正确地将PHP代码从版本5.6重写到7.x?

[英]How to properly rewrite php code from version 5.6 to 7.x?

我自己绑了它,但对我不起作用。 例如,如何正确更改此旧代码以与php 7.x兼容?

class DbSimple_Generic
{
    function& connect($dsn)
    {
        // Load database driver and create its instance.
        $parsed = DbSimple_Generic::parseDSN($dsn);
        if (!$parsed) {
            $dummy = null;
            return $dummy;
        }
        $class = 'DbSimple_'.ucfirst($parsed['scheme']);
        if (!class_exists($class)) {
            $file = str_replace('_', '/', $class) . ".php";
            if ($f = @fopen($file, "r", true)) {
                fclose($f);
                require_once($file);
            } else {
                $base = basename($file);
                $dir = dirname(__FILE__);
                if (@is_file($path = "$dir/$base")) {
                    require_once($path);
                } else {
                    trigger_error("Error loading database driver: no file $file in include_path; no file $base in $dir", E_USER_ERROR);
                    return null;
                }
            }
        }
        $object =& new $class($parsed);   
        if (isset($parsed['ident_prefix'])) {
            $object->setIdentPrefix($parsed['ident_prefix']);
        }



class DbSimple_Mysql_Blob extends DbSimple_Generic_Blob
{
    var $blobdata = null;
    var $curSeek = 0;

    function DbSimple_Mysql_Blob(&$database, $blobdata=null)
    {
        $this->blobdata = $blobdata;
        $this->curSeek = 0;
    }

    function read($len)
    {
        $p = $this->curSeek;
        $this->curSeek = min($this->curSeek + $len, strlen($this->blobdata));
        return substr($this->blobdata, $this->curSeek, $len);
    }

    function write($data)
    {
        $this->blobdata .= $data;
    }

    function close()
    {
        return $this->blobdata;
    }

    function length()
    {
        return strlen($this->blobdata);
    }
}        

function& _performNewBlob($blobid=null)
{
    $obj =& new DbSimple_Mysql_Blob($this, $blobid);
    return $obj;
}

我试图使用各种可能的方法来使这项工作像这样:

$object = new $class($parsed);
$object->method();

对于PHP 7.x,这似乎是最有问题的部分:

$object =& new $class($parsed);

但这行不通。 我试图在一些PHP文档中找到它,但是到目前为止还没有运气。 那么如何正确重写呢? 谢谢

在具有Apache和mysql的Ubuntu Server 64位16.04+上使用此功能。

只是永远不要使用=&运算符。 自PHP 5.0以来,它一直没有用,并在PHP 7.0中删除:

http://php.net/manual/zh/migration70.incompatible.php#migration70.incompatible.other.new-by-ref

您会在该页面上找到更多在PHP 7中不再起作用的内容。

了解该功能并以更简洁的方式重写可能是一个更好的主意,但是请在下面的更改中查找,希望对您有所帮助。

class DbSimple_Generic
{
    function connect($dsn)
    {
        // Load database driver and create its instance.
        $parsed = DbSimple_Generic::parseDSN($dsn);
        if (!$parsed) {
            $dummy = null;
            return $dummy;
        }
        $class = 'DbSimple_'.ucfirst($parsed['scheme']);
        if (!class_exists($class)) {
            $file = str_replace('_', '/', $class) . ".php";
            if ($f = @fopen($file, "r", true)) {
                fclose($f);
                require_once($file);
            } else {
                $base = basename($file);
                $dir = dirname(__FILE__);
                if (@is_file($path = "$dir/$base")) {
                    require_once($path);
                } else {
                    trigger_error("Error loading database driver: no file $file in include_path; no file $base in $dir", E_USER_ERROR);
                    return null;
                }
            }
        }
        $object = new $class($parsed);   
        if (isset($parsed['ident_prefix'])) {
            $object->setIdentPrefix($parsed['ident_prefix']);
        }
    }      
    public static function parseDSN($dsn){ // public or private depends on what you intend to do
       // implementation here...  
    }

    public function setIdentPrefix($identPrefix){
       // implementation here...
    }
}

class DbSimple_Mysql_Blob extends DbSimple_Generic_Blob
{
    var $blobdata = null;
    var $curSeek = 0;

    function __construct($blobdata=null)  // use __construct for class constructor
    {
        $this->blobdata = $blobdata;
        $this->curSeek = 0;
    }

    function read($len)
    {
        $p = $this->curSeek;
        $this->curSeek = min($this->curSeek + $len, strlen($this->blobdata));
        return substr($this->blobdata, $this->curSeek, $len);
    }

    function write($data)
    {
        $this->blobdata .= $data;
    }

    function close()
    {
        return $this->blobdata;
    }

    function length()
    {
        return strlen($this->blobdata);
    }
}        

function _performNewBlob($blobid=null)
{
    $obj = new DbSimple_Mysql_Blob($blobid); // no need to use &
    return $obj;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM