php - 教义记录的范围是什么?
问题描述
我正在调查一个错误,它看起来像 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
。
我非常想知道为什么会发生这种情况以及为什么这种行为是可取的。
解决方案
推荐阅读
- javascript - React Hooks 状态更新仅在从组件数组中的回调调用时应用一次
- authentication - dokku - 授予另一个用户 root 访问权限
- c# - .Net Core 3.1 API 无法通过 IIS 上托管的 Windows 身份验证访问网络共享
- influxdb - 标签上的 Influxdb 查询不返回任何内容
- python - 使用自定义生成器(img,csv)的损失的奇怪行为
- python - 需要帮助理解以下 python 字符串提取
- java - 如何在 DOT [.] 之后删除文本
- angular - 更改后如何以角度更新单个变量
- rust - Rust 实现迭代器
- javascript - 将数组字符串分配给某些链接