<?php
// 关键词: 访问控制符, public, protected, private
// 1. public: 默认, 类内,类外,子类都可见
// 2. protected: 类内, 子类可见, 类外不可见
// 3. private: 类内可见, 子类, 类外不可见
class Demo1{
public $name ;
protected $dept;
private $salary;
public function __construct($name,$dept,$salary)
{
$this->name=$name;
$this->dept=$dept;
$salary->salary=$salary;
}
public function getDept(){
return $this->dept ==='技术部'?$this->dept:'无权查看';
}
public function getSalary(){
return $this->dept==='财务部'?$this->salary:'无权查看';
}
}
$obj = new Demo1('超', '技术部', 9999);
$obj1 = new Demo1('高', '财务部', 6666);
echo $obj->name, '<br>';
echo $obj->dept, '<br>';//外部不能访问
echo $obj->salary,'<br>';//外部不能访问
echo $obj->getPosition(), '<br>';
echo $obj1->getPosition(), '<br>';
echo $obj->getSalary(), '<br>';
echo $obj1->getSalary(), '<br>';
class Demo2 extends Demo1{
public function salary(){
return $this->salary;
}
}
$obj3 = new Demo2('超','技术部',8888);
echo $obj3->salary();//子类不允许访问private变量