首页 > 解决方案 > 教义记录的范围是什么?

问题描述

我正在调查一个错误,它看起来像 Doctrine 在返回 Record 对象时不尊重变量范围的想法。

<?php

class Thing {
    var $variable;

    public function doStuff() {
        $thing1 = self::getById(1);
        echo($thing1->variable); 
        $thing1->variable = "new value";
        Thing::doSomeOtherStuff();
        echo($thing1->variable);
    }

    public static function doSomeOtherStuff() {
        $thing2 = Thing::getById(1);
    }

    /**
     * this runs a query against a database and 
     * returns a Thing object with $variable = "old value" 
     */ 
    public static function getById($id): Thing {
        return Doctrine_Query::CREATE()
            ->select('t.*')
            ->from('Thing t')
            ->where('t.id = ?', $id) 
            ->fetchOne([], Doctrine_Core::HYDRATE_RECORD);
    }
}

预期输出:

old value
new value

实际输出:

old value 
old value 

使用HYDRATE_ARRAY. 使用调试器,这种方法看起来是在对$thing1创建时的值进行实际更新$thing2

我非常想知道为什么会发生这种情况以及为什么这种行为是可取的。

标签: phpdoctrine

解决方案


推荐阅读