小编Mat*_*sen的帖子

如何在 PHP 8.1 中使用反射更改只读属性?

有没有办法使用反射或其他方式来更改已设置的只读属性?

我们有时会在测试中这样做,并且我们不想避免仅出于测试目的而使用只读属性。

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)

php reflection php-8 php-8.1

8
推荐指数
1
解决办法
2185
查看次数

如何同时使用Elasticsearch的geo_point和geo_shape类型?

这是我们的文件:

{
    "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)

elasticsearch

6
推荐指数
1
解决办法
1260
查看次数

标签 统计

elasticsearch ×1

php ×1

php-8 ×1

php-8.1 ×1

reflection ×1