- 论坛徽章:
- 1
|
- <?php
- Class struct{
- private $v = array(
- 'a'=>array('int', 0, null),
- 'b'=>array('float', 0, null),
- 'c'=>array('string','50', null),
- );
- private function __set($nm, $val){
- if (isset($this->v[$nm]))
- {
- settype($this->v[$nm][2] = $val,$this->v[$nm][0]);
- if ($this->v[$nm][0] == 'string')
- {
- $this->v[$nm][2] = substr($this->v[$nm][2] . str_repeat(' ',$this->v[$nm][1]),0,$this->v[$nm][1]);
- }
- }
- else
- {
- throw new Exception("$nm isn't my member!");
- }
- }
- private function __get($nm){
- if (isset($this->v[$nm]))
- {
- return $this->v[$nm][2];
- }
- else
- {
- throw new Exception("$nm isn't my member!");
- }
- }
- }
- $s1 = new struct();
- $s2 = new struct();
- $s1->a = 1;
- $s2->a = 2;
- $s1->b = 3.3;
- $s2->b = 4.4;
- $s1->c = "test1";
- $s2->c = "test2";
- var_dump($s1->c);
- var_dump($s2->c);
复制代码
可以这么做,只是一个例子。 |
|