dongyao4003 2015-11-08 14:59
浏览 21
已采纳

括号和对象成员的PHP行为

Recently I was working with results from an SQL query call on a mysqli object. I wrote this line of code:

$record = ($result->fetch_object())->acc_id;

and i got Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) for that line.

Once i changed the code to this:

$record = $result->fetch_object()->acc_id;

it worked as intended.

Why did the parenthesis induce a parse error? I know that in C# the former code would work regardless of them (accounting for the change in syntax)?

  • 写回答

1条回答 默认 最新

  • dongtidai6519 2015-11-18 09:51
    关注

    As stated by trincot:

    Limitation of PHP. You can not use the -> on an evaluated expression

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?