Home > Article > Backend Development > Alternative usage of classes--data encapsulation_PHP tutorial
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");
?>