在PHP中,什么是临时表达式?

huangapple go评论95阅读模式
英文:

What is a temporary expression in PHP?

问题

当我运行以下代码时,

<?php

const nums = [1, 2, 3];
nums[0] = 50;

它给我返回以下错误:

Fatal error: Cannot use temporary expression in write context in <file> on line 4

我的问题是,在PHP中,"temporary expression" 究竟是什么意思?我已经尝试在PHP文档中搜索答案,但没有关于这个错误的具体说明。

我只是不明白这个错误究竟是什么意思?

英文:

When I run the following code,

<?php

const nums = [1, 2, 3];
nums[0] = 50;

it gives me the following error:

Fatal error: Cannot use temporary expression in write context in <file> on line 4

My question is what exacty is a temporary expression in PHP? I've tried searching the documentation of PHP for an answer but there is nothing mentioned there regarding this error.

I just can't understand what exactly does this error mean?

答案1

得分: 2

这条消息是泄露内部信息的一部分。如果你在PHP源代码中查找这个字符串,你会找到明确的单元测试,说明在这些情境下会抛出错误:

  • 枚举防止取消设置值

    枚举 Foo: int {
        项目 栏 = 0;
    }
    
    取消设置(Foo::Bar->value);
    
  • 临时引用传递的尺寸提取是不允许的

    $fn = function(&$ref) {};
    $fn([0, 1][0]);
    
  • 临时引用传递的属性提取是不允许的

    $fn = function(&$ref) {};
    $fn([0, 1]->prop);
    
  • 不允许对临时表达式进行写入

    [0, 1][0] = 1;
    

这条消息让人困惑,因为它不是由对常量的直接写入触发的。事实上,单独的 FOO = 2; 就是一个解析错误。

英文:

This message is a bit of a leaked internal. If you look for the string in PHP source code, you'll find explicit unit tests that illustrate the error being thrown in these contexts:

  • Enum prevent unsetting value

    enum Foo: int {
        case Bar = 0;
    }
    
    unset(Foo::Bar->value);
    
  • Passing a dimension fetch on a temporary by reference is not allowed

    $fn = function(&$ref) {};
    $fn([0, 1][0]);
    
  • Passing a property fetch on a temporary by reference is not allowed

    $fn = function(&$ref) {};
    $fn([0, 1]->prop);
    
  • Writing to a temporary expression is not allowed

    [0, 1][0] = 1;
    

The message is confusing because it isn't directly triggered by writing to a constant. In fact, FOO = 2; alone is a parse error.

huangapple
  • 本文由 发表于 2023年6月29日 16:04:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定