php灭绝师太私密亲传面向对象(OOP)编程
<?php
class Person{
public $Name;
protected $Age;
public $Job;
//构造函数 一般用于初始化
public function __construct($name,$age,$job){
$this->Name = $name;
$this->Age = $age;
$this->Job = $job;
}
//对外访问方法,用于对外访问不到受保护的,及私有的属性
public function getAge(){
return $this->Age;
}
}
//实例化Person 类
$wang = new Person("王小明",24,"摸鱼的");
$li = new Person("李大明",35,"凑数的");
$chen = new Person("陈楚生",36,"歌手");
$liu = new Person("刘德华",59,"演员");
//Age属性是受保护的,出本类以及派生类外,都无法访问,但是可以用访问器进行访问
// echo $wang->Age;
echo $wang->Name;
echo $wang->getAge();
echo $wang->Job;
echo "<hr/>";
echo $li->Name;
echo $li->getAge();
echo $li->Job;
echo "<hr/>";
echo $chen->Name;
echo $chen->getAge();
echo $chen->Job;
echo "<hr/>";
echo $liu->Name;
echo $liu->getAge();
echo $liu->Job;