When do/should I use __construct(), __get(), __set(), and __call() in PHP?

后端 未结 10 709
再見小時候
再見小時候 2020-12-05 08:16

A similar question discusses __construct, but I left it in my title for people searching who find this one.

Apparently, __get and __set take a parameter that is the

10条回答
  •  隐瞒了意图╮
    2020-12-05 09:16

    __get(), __set(), and __call() are what PHP calls "magic methods" which is a moniker I think that is a bit silly - I think "hook" is a bit more apt. Anyway, I digress...

    The purpose of these is to provide execution cases for when datamembers (properties, or methods) that are not defined on the object are accessed, which can be used for all sorts of "clever" thinks like variable hiding, message forwarding, etc.

    There is a cost, however - a call that invokes these is around 10x slower than a call to defined datamembers.

提交回复
热议问题