class StrictCoordinateClass
{
private $arr=array('x'=>NULL,'y'=>NULL);
function __get($property)
{
if(array_key_exists($property, $this->arr))
{
return $this->arr[$property];
}else
{
print "Error: Can't read a property other than x & y\n";
}
}
function __set($property,$value)
{
if(array_key_exists($property, $this->arr))
{
$this->arr[$property]=$value;
}else
{
print "Error: Can't write a property other than x & y\n";
}
}
}
$obj=new StrictCoordinateClass();
$obj->x=1;
print $obj->x;
print "\n";
$obj->n=2;
print $obj->n;
__get和__set的用法
最新推荐文章于 2021-09-21 15:29:34 发布