PHP variable scope within Try/Catch block

后端 未结 3 1293
孤独总比滥情好
孤独总比滥情好 2021-02-07 09:06

In PHP, how do variable scope rules apply to Try/Catch blocks? Do variables declared within the try block go out of scope when the block has finished? Or are they i

3条回答
  •  爱一瞬间的悲伤
    2021-02-07 10:03

    I believe this is opinion based mostly. The code is correct and it will work as expected as long as the catch block always has the return statement. if the catch block does not return, the flow will continue and the code outside the try/catch block will be executed, and it will fail, because $o won't be initialized. You will be able to access $o because of the lack of block scope in php, but the method won't exist because the object construction failed.

提交回复
热议问题