PHP变量虽然没有通过引用传递,但会自行更改

时间:2016-01-02 22:22:22

标签: php

在下面的代码中,我无法理解为什么变量$service_category_tree会自行更改。不知何故,subscription_tree改变了参数变量,尽管它没有通过引用传递。

$service_category_tree = $this->service_category_model->with_request_forms();
foreach ($this->view_data['users'] as &$user) {
    echo md5(serialize($service_category_tree))."<br>"; // To check if the variable is changed.
    $user->service_categories = $this->merchant_model->subscription_tree($user->id, $service_category_tree);
}

如您所见,MD5语句的输出显示变量已更改:

10066a31bb6e191fbb94b8dc61fd6e55
10066a31bb6e191fbb94b8dc61fd6e55
fd1e22aa74c048e88751c85528d23254
752e0cef4357d7a33e47b90ef207d396
8b5b6ea856080bf32b9bfecc4e43a3f6
8b5b6ea856080bf32b9bfecc4e43a3f6
8b5b6ea856080bf32b9bfecc4e43a3f6
3f3e7619e59e3776c395065e4278c3f8
3f3e7619e59e3776c395065e4278c3f8
dbfebae62d65bd04ce4ce74f2046f90e
34498709256b34ce4d843c1f735c4c6a

方法subscription_tree在第二个参数上没有&符号,如下面的代码所示。

function subscription_tree($merchant_id, $tree_array = NULL){
    if($tree_array === NULL){
        $this->load->model('service_category_model');
        $tree_array = $this->service_category_model->with_request_forms();
    }
    if(is_array($tree_array) && count($tree_array)){
        $my_request_forms = $this->request_forms($merchant_id)->result();
        $my_service_categories = $this->service_categories($merchant_id)->result();
        foreach ($tree_array as &$sc) {

            // Indicate subscribed service categories
            $sc->subscribed = FALSE;
            if(is_array($my_service_categories) && count($my_service_categories)) {
                foreach ($my_service_categories as $my_sc_key => $my_sc) {
                    if($my_sc->id == $sc->id){
                        $sc->subscribed = TRUE;
                        unset($my_service_categories[$my_sc_key]);
                    }
                }
            }

            // Indicate subscribed request forms
            if(is_array($sc->request_forms) && count($sc->request_forms)) {
                foreach ($sc->request_forms as &$rf) {
                    $rf->subscribed = FALSE;
                    if(count($my_request_forms)) {
                        foreach($my_request_forms as $mrf_key => $my_request_form){
                            if ( $my_request_form->id == $rf->id ) {
                                $rf->subscribed = TRUE;
                                unset($my_request_forms[$mrf_key]);
                                break;
                            }
                        }
                    }
                }
            }

        }
    }
    return $tree_array;
}

1 个答案:

答案 0 :(得分:2)

在PHP中,对象总是通过引用传递:

如此行

foreach ($tree_array as &$sc) {

        // Indicate subscribed service categories
        $sc->subscribed = FALSE;

可以更改$tree_array对象,这是对$service_category_tree

的引用

您还可以制作阵列的深层复制

$new_tree = array();

foreach ($service_category_tree as $k => $v) {
    $new_tree[$k] = clone $v;
}

并将其传递给该函数。或者在函数中做一个深层复制......

http://php.net/manual/en/language.oop5.references.php

的更多信息