简体   繁体   English

如何在升级到5.6.x后修复php中的引用传递

[英]How to fix pass by reference in php after upgrading to 5.6.x

I recently upgraded fom php 5.2 to 5.6 and there is some code I could not fix yet: 我最近将fom php 5.2升级到5.6,还有一些我无法解决的代码:

//Finds users with the same ip- or email-address
function find_related_users($user_id) {
    global $pdo;

    //print_R($pdo);

    //Let SQL do the magic!
    $sth = $pdo->prepare('CALL find_related_users(?)');
    $sth->execute(array($user_id));
    //print_R($sth);
    //Contains references to all users by id, to check if a user has already been processed
    $users_by_id = array(); 

    //Contains arrays of references to users by depth
    $users_by_depth = array();

    while ($row = $sth->fetchObject()) {
        //Create array for current depth, if not present
        if (!isset($users_by_depth[$row->depth])) 
            $users_by_depth[$row->depth] = array();

        //If the user is new
        if (!isset($users_by_id[$row->id])) {
            //Create user array
            $user = array(
                'id' => $row->id,
                'name' => $row->name,
                'email' => $row->email,
                'depth' => $row->depth,
                'adverts' => array()
            );

            //Add all users to depth array
            @array_push($users_by_depth[$row->depth], &$user);

            //Add references to all users to id array (necessary to check if the id has already been processed)
            $users_by_id[$row->id] = &$user;
        }
        //If user already exists
        else 
            $user = &$users_by_id[$row->id];

        //Add advert to user
        if ($row->advert_id != null)
            array_push($user['adverts'], array(
                'id' => $row->advert_id,
                'title' => $row->advert_title,
                'msgs' => $row->msgs,
                'url' => $row->url
            ));
        #print_r($user);
        //Unset $user variable !!! 
        //If this is missing, all references in the array point to the same user
        unset($user);
    }

    //Return users, grouped by depth
    return $users_by_depth;
}

If I simply remove the ampersand before the dollar sign, the function stops to work as intended. 如果我只是在美元符号之前删除&符号,则该函数将停止按预期工作。 From other questions on stackoverflow I found that this is a call by reference and will brake for new php versions. 从stackoverflow上的其他问题我发现这是一个引用调用,并将为新的PHP版本制动。 However I could not find a solution yet. 但是我找不到解决方案了。

Thank you for any help on how to update this code for php 5.6.x 感谢您提供有关如何更新php 5.6.x此代码的任何帮助

Your code was probably never working as you thought it was as you are suppressing the errors on your array_push() call. 您的代码可能永远不会像您认为的那样工作,因为您正在抑制array_push()调用上的错误。 Note that only the first parameter of array_push() is passed by reference, the other values are always passed by value. 请注意,只有array_push()的第一个参数通过引用传递,其他值总是按值传递。

You should remove the error suppressor @ (never use that in your own code) and in this case you can also do: 你应该删除错误抑制器@ (永远不要在你自己的代码中使用它),在这种情况下你也可以这样做:

$users_by_depth[$row->depth][] = &$user;
                            ^^ add an element just like `array_push`

Now your new value in your $users_by_depth will contain a reference to the $user variable. 现在, $users_by_depth新值将包含对$user变量的引用。

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

相关问题 在CentOS 6.7上从php 5.5.x升级到5.6.x. - Upgrading from php 5.5.x to 5.6.x on CentOS 6.7 将Concrete5从5.5.2.1升级到5.6.x的问题 - Issues upgrading Concrete5 from 5.5.2.1 to 5.6.x 将PHP 5.5.x更新到5.6.x后,在OS X El Capitan上更新PHP的路径 - Update path to PHP on OS X El Capitan after updating PHP 5.5.x to 5.6.x PHP 5.6.x失去了连接ODBC源的能力 - PHP 5.6.x lost ability to connect to ODBC source PHP 5.3.x的安全性是否低于PHP 5.6.x - Is PHP 5.3.x less secure than PHP 5.6.x 使用代码点火器使用PHP 5.6.x(xamp)配置MSSQL - configuration MSSQL with PHP 5.6.x(xamp) using code-igniter PHP 5.6.x源与Apache一起安装,但是发生opensl错误,将指针解引用为不完整类型 - PHP 5.6.x source install with Apache, but openssl error occurs, dereferencing pointer to incomplete type 开发服务器不会停止缓存(Vagrant - Apache 2.4 - Centos 7.6 - php 5.6)在 MacOSX 上。 - DEVELOPMENT server won't stop caching (Vagrant - Apache 2.4 - Centos 7.6 - php 5.6.x ) on MacOSX 从php 5.3升级到5.6后的PHP错误 - PHP error after upgrading from php 5.3 to 5.6 phpMyAdmin - 将 php 5.6 升级到 php 7 后出现错误:(缺少 mysqli 扩展) - phpMyAdmin - Error : (The mysqli extension is missing) after upgrading php 5.6 to php 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM