分类: LINUX
2008-11-27 17:06:24
php
class Setter
{ public $n;
private $x = array("a" => 1, "b" => 2, "c" => 3);
private function __get($nm)
{ echo "Getting [$nm]n";
if (isset($this->x[$nm]))
{ $r = $this->x[$nm];
print "Returning: $rn";
return $r;
} else { echo "Nothing!n"; }
}
private function __set($nm, $val)
{ echo "Setting [$nm] to $valn";
if (isset($this->x[$nm]))
{ $this->x[$nm] = $val;
echo "OK!n";
} else { echo "Not OK!n"; }
}
private function __isset($nm)
{ echo "Checking if $nm is setn";
return isset($this->x[$nm]);
}
private function __unset($nm)
{ echo "Unsetting $nmn";
unset($this->x[$nm]);
}
}
$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump(isset($foo->a)); //true
unset($foo->a);
var_dump(isset($foo->a)); //false
// this doesn't pass through the __isset() method
because 'n' is a public property
var_dump(isset($foo->n));
var_dump($foo);
?>
php
class Caller
{ private $x = array(1, 2, 3);
public function __call($m, $a)
{ print "Method $m called:n";
var_dump($a);
return $this->x;
}
}
$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);