php automated setter and getter

会有一股神秘感。 提交于 2019-12-03 01:24:59

If you want to simulate the getXy and setXy functions for arbitrary properties, then use the magic __call wrapper:

function __call($method, $params) {

     $var = lcfirst(substr($method, 3));

     if (strncasecmp($method, "get", 3) === 0) {
         return $this->$var;
     }
     if (strncasecmp($method, "set", 3) === 0) {
         $this->$var = $params[0];
     }
}

This would be a good opportunity to do something useful for once, by adding a typemap or anything. Otherwise eschewing getters and setters alltogether might be advisable.

read the magic functions of php and your need you can use __get and __set functions

read this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!