Home  >  Article  >  php教程  >  类的另类用法--数据的封装_php基础

类的另类用法--数据的封装_php基础

WBOY
WBOYOriginal
2016-05-17 09:10:17662browse

类的另类用法--数据的封装
一般的情况下,如果使用classname::property是不能访问到类的属性的,但可以用classname::method()使用类的方法。同样的也不能用objectname->property访问到类的方法里的变量。利用这一特点,我们可以将一些数据保存于类中,有点象c++的私有属性。

class data {
  function value($var) {
    static $d = array();
    if(func_num_args() > 1) {
      $d[$var] = func_get_arg(1);
    }else {
      return $d[$var];
    }
  }
}
//测试:
data::value("a",1);
data::value("b",2);
echo data::value("a");
echo data::value("b");
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn