<?php
class A
{
private $name = 'hello world';
function __set($property_name,$value)
{
$this->$property_name=$value;
}
function __get($property_name)
{
if (isset($this->$property_name))
{
return ($this->$property_name);
}
else
{
return NULL;
}
}
}
$a = new A();
$a->name='ni hao';
echo $a->name;
?>
<?php
class Person{
private $age;
function setAge($age) // 为外部提供一个公有设置年龄的方法
{
if ($age<0 || $age>130)
return;
$this->age = $age;
}
function getAge() // 为外部提供一个公有获取年龄的方法
{
return($this->age);
}
}
$man=new Person();
$man->setAge(100);
echo '这个人的年龄是 '.$man->getAge().'岁';
?>