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

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

    Redefining __get and __set can be especially useful in core classes. For example if you didn't want your config to be overwritten accidentally but still wanted to get data from it:

    class Example
    {
        private $config = array('password' => 'pAsSwOrD');
        public function __get($name)
        {
            return $this->config[$name];
        }
    }
    

提交回复
热议问题