Correcting teacher:灭绝师太
Correction status:qualified
Teacher's comments:
OOP:final, self, $this举例使用
案例效果:
<?php
// 特殊的对象引用$this,代表本对象,专门完成对象内部成员的访问
class people{
public $name;
public $age;
public $height;
public function __construct($name = "" , $age = "",$height = "")
{
$this->name = $name;
$this->age = $age;
$this->height = $height;
}
public function program()
{
return $this->name . ', 年龄: ' . $this->age. '身高:' . $this->height.'<br>';
}
}
$fugen = new people("张福根","18岁","188CM");
echo $fugen->name,'<br>';
echo $fugen->program();
<?php
// self是类内部代替类名的关键字,
// self::静态成员 完成本类中静态成员的访问,
// * 类名::静态成员 完成类外部和成员方法中静态成员的访问
class User{
public $name = "张福根";
public static $age = 21;
static function getAge()
{
echo '年龄:'.User::$age.'<br>';
echo '年龄:'.self::$age.'<br>';
}
}
User::getAge();
`
<?php
// final:加在类前,表示该类不能被继承,加在方法前,表示该方法不能被重写
final class BaseClass {
public function test() {
echo " my name is fugen<br>";
}
// 这里无论是否将方法声明为final,都没有关系
final public function moreTesting() {
echo "我 is 福根<br>";
}
}
class ChildClass extends BaseClass {
}
$fugen =new BaseClass();
$fugen->test();
// Fatal error: Class ChildClass may not inherit from final class (BaseClass)