📄 07c08-3.php
字号:
<?php// Define a class that will store values for any property it is given,// and format them nicelyclass Data_Formatter { // The data storage array private $data = array(); // Method to set any variable passed to this object. public function __set($name, $value) { if (is_int($value)) { // It is an integer, format it with commas, but no decimal $new_value = number_format($value, 0); } elseif (is_float($value)) { // It is a float value, format it with commas, and 2 decimals: $new_value = number_format($value, 2); } elseif (is_bool($value)) { // A boolean value, turn it into the string 'true' or 'false' $new_value = $value ? 'true' : 'false'; } else { // Otherwise just store the data as is: $new_value = $value; } // Now store the data in our array $this->data[$name] = $new_value; } // A method to return any values that are set: public function __get($name) { if (isset($this->data[$name])) { return $this->data[$name]; } } // A method that will allow 'isset' to work on these variables public function __isset($name) { return isset($this->data[$name]); } // Finally a method to allow 'unset' calls to work on these variables public function __unset($name) { unset($this->data[$name]); }}// Create a data holding object & store some data in it:$data = new Data_Formatter();$data->number = 1200;$data->money = 189000.452;$data->lies = false;$data->place = 'Mount Airy';// Now echo these values out to see their formatted values:// 1,200 189,000.45 false Mount Airyecho "<p>The value of 'number' is {$data->number}</p>";echo "<p>The value of 'money' is {$data->money}</p>";echo "<p>The value of 'lies' is {$data->lies}</p>";echo "<p>The value of 'place' is {$data->place}</p>";?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -