Home > Backend Development > PHP Tutorial > 类的另类用法--数据的封装

类的另类用法--数据的封装

WBOY
Release: 2016-06-01 14:28:54
Original
758 people have browsed it
类的另类用法--数据的封装
一般的情况下,如果使用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");
?>

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template