Home  >  Article  >  Backend Development  >  Alternative usage of classes--data encapsulation_PHP tutorial

Alternative usage of classes--data encapsulation_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:07:48735browse

Alternative usage of classes - data encapsulation
Under normal circumstances, if you use classname::property, you cannot access the properties of the class, but you can use classname::method() to use the class method. Similarly, you cannot use objectname->property to access variables in class methods. Taking advantage of this feature, we can save some data in the class, a bit like private properties in C++.
class data {
function value($var) {
static $d = array();
if(func_num_args() > 1) {
$ d[$var] = func_get_arg(1);
}else {
  return $d[$var];
}
}
}
//Test:
data::value("a",1);
data::value("b",2);
echo data::value("a");
echo data::value( "b");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315048.htmlTechArticleAlternative usage of the class - data encapsulation Under normal circumstances, if classname::property is used, it cannot be accessed Class attributes, but you can use classname::method() to use class methods. Same...
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