有没有办法使用反射或其他方式来更改已设置的只读属性?
我们有时会在测试中这样做,并且我们不想避免仅出于测试目的而使用只读属性。
class Acme {
public function __construct(
private readonly int $changeMe,
) {}
}
$object= new Acme(1);
$reflectionClass = new ReflectionClass($object);
$reflectionProperty = $reflectionClass->getProperty('changeMe');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, 2);
Run Code Online (Sandbox Code Playgroud)
Fatal error: Uncaught Error: Cannot modify readonly property Acme::$changeMe
Run Code Online (Sandbox Code Playgroud) 这是我们的文件:
{
"geometry" : {
"type" : "Point",
"coordinates" : [ -87.662682, 41.843014 ]
}
}
Run Code Online (Sandbox Code Playgroud)
我们希望做一个geo_shape
与搜索_geo_distance
排序,都对同一geometry
领域.前者需要geo_shape
类型,而后者需要geo_point
.
这两个索引单独成功,但不能一起成功:
"geometry": {
"type": "geo_shape"
}
Run Code Online (Sandbox Code Playgroud)
和
"geometry": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
},
Run Code Online (Sandbox Code Playgroud)
到目前为止,我们尝试过这些并且失败了:
"geometry": {
"type": "geo_shape"
},
"geometry.coordinates": {
"type": "geo_point"
},
Run Code Online (Sandbox Code Playgroud)
也
"geometry": {
"copy_to": "geometryShape",
"type": "geo_shape"
},
"geometryShape": {
"properties": {
"coordinates": {
"type": "geo_point" …
Run Code Online (Sandbox Code Playgroud)