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

后端 未结 10 733
再見小時候
再見小時候 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

    Probably not the cleanest design in the world but I had a situation where I had a lot of code that was referencing an instance variable in a class, i.e.:

    $obj->value = 'blah';
    echo $obj->value;
    

    but then later, I wanted to do something special when "value" was set under certain circumstances so I renamed the value variable and implemented __set() and __get() with the changes I needed.

    The rest of the code didn't know the difference.

提交回复
热议问题