public $name; public $sex; public $iq=10; const BIRTHPLACE='地球'; //构造函数 public function __construct($name,$sex){ $this->name=$name; $this->sex=$sex; } protected function chew($food){ echo "{$food}已经被咀嚼完成!
"; } public function eat($food){ $this->chew($food); echo "{$this->name}正在吃{$food}!
"; } public function hello(){ echo '您好!我是来自'.self::BIRTHPLACE.'的人类
'; } } class Student extends Humanity { const BIRTHPLACE='火星'; public $studentId; public function test($subject){ echo "{$this->name}正在考{$subject}!
"; } public function eat($food){ $this->chew($food); echo "{$this->name}正在快速的吃{$food}!
"; } public function hello(){ parent::chew('铅笔');//没有必要通过这种方式来访问,因为他以及被你继承过来了! parent::hello(); echo '您好!我是来自'.self::BIRTHPLACE.'人类中的学生
'; } } echo ''.Humanity::BIRTHPLACE.'
'; echo ''.Student::BIRTHPLACE.'
'; //$hanMM=new Humanity('韩梅梅','女'); //$hanMM->hello(); $liLei=new Student('李雷','男'); $liLei->hello(); echo $liLei::BIRTHPLACE;//可以,但是我们极度不建议你通过具体的实例去访问 类常量
解析: **范围解析操作符:: 作用: 一、访问类里面的静态成员
二、访问类里面的常量 类内部如果访问? self::类常量名称
parent::类常量名称 类外部如果访问? 类名称::类常量名称 注意点:需要访问那个类里面的常量就用哪个类的名称就可以了
三、在子类里面访问父类中的方法(被重写了的方法)
parent::方法
我们没有必要在子类里面通过parent去访问父类中 没有被 重写 的方法!
注意:类常量可以在子类中重写去定义、但是不能直接修改其值!**
效果: