self:: vs className:: inside static className methods in PHP

前端 未结 2 1586
别跟我提以往
别跟我提以往 2021-02-07 02:52

I guess there may not be any difference but personal preference, but when reading various PHP code I come across both ways to access the methods class.

What is

2条回答
  •  清歌不尽
    2021-02-07 03:35

    (Note: the initial version said there was no difference. Actually there is)

    There is indeed a small diference. self:: forwards static calls, while className:: doesn't. This only matters for late static bindings in PHP 5.3+.

    In static calls, PHP 5.3+ remembers the initially called class. Using className:: makes PHP "forget" this value (i.e., resets it to className), while self:: preserves it. Consider:

提交回复
热议问题