<?php
class Demo
{
public $name;
public $age;
public $salary;
private $sex;
private $ismarried;
public static $home;
public function __construct($name,$age,$salary,$sex,$ismarried,$home)
{
$this->name=$name;
$this->age=$age;
$this->salary=$salary;
$this->sex=$sex;
$this->ismarried=$ismarried;
self::$home=$home;
}
public function query()
{
print '遍历出对象中的所有属性,包括私有和受保护的属性:<br>';
foreach ($this as $key => $value) {
print $key.'=>'.$value.'<br>';
}
print self::$home;
}
}
$obj = new Demo('xiatao',42,4880,'male',true,'镇江');
echo '外部访问公共属性:<br>';
foreach ($obj as $key => $value) {
echo $key.'=>'.$value.'<br>';
}
echo Demo::$home;
echo '<hr>';
$obj->query();
echo '<hr>';
?>