An alternative usage of class-data encapsulation. Alternative usage of classes-data encapsulation generally, if classname: property is used, the class attributes cannot be accessed, but classname: method () can be used () use the class method. Alternative usage of the same class-data encapsulation
Generally, if you use classname: property, you cannot access the attributes of the class, but you can use classname: method () to use the class method. Similarly, you cannot use objectname-> property to access the variables in the class method. With this feature, we can save some data in the class, a bit like the private property of 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 ("");
Echo data: value ("B ");
?>
In general, if you use classname: property, you cannot access the class attributes, but you can use classname: method () to use the class method. The same...