PHP封装

来源:互联网 发布:java udp收数据不全 编辑:程序博客网 时间:2024/05/21 08:50
<?php
class Person{
public $name;
protected $age;
private $salary;

function __construct($name,$age,$salary){
$this->name=$name;
$this->age=$age;
$this->salary=$salary;
}
//我们可以通过方法来访问protected或者private变量
//获取薪水
public function getSalary($user,$pass){
if($user=="kong"&&$pass="123"){
return $this->salary;
}else{
return "sorry,你无权查看";
}
}

//修改年龄
public function setAge($age){
$this->age=$age;
}

//查看年龄
public function getAge(){
return $this->age;
}
}

//创建一个人
$p1=new Person("小孔",22,2000);

echo $p1->name;
echo $p1->getSalary("kong","123");

//修改年龄
$p1->setAge(23);
echo $p1->getAge();
?>
0 0
原创粉丝点击