<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> </body> </html><?php /** * */ class Ren { private $name; private $sex; private $age; public static $aa; //封装:访问成员变量的方法 /* public function setAge($age) { if($age>18&&$age<80) { $this->age=$age; } }*/ //封装:简便方法赋值 public function __set($name,$value) { if($name == "age") { if($value>18 && $value<80) { $this->$name = $value; } } else { $this->$name = $value; } } //封装:简便方法获取值 public function __get($zw) { return $this->$zw; } public function say () { echo $this->name."这个人在讲话"; } //构造函数,初始化 function __construct($name ,$sex) { $this->name=$name ; $this->name=$sex; } //析构函数,在对象要销毁的时候自动调用,可以在里面进行释放内存,关闭连接等 function __destruct(){ echo "该对象马上要销毁了"; } } //继承 class Student extends Ren { public $class; //父类方法重写 function Say() { parent::Say(); //调用父类中的方法 echo "这是子类中的方法"; } } //静态 class AA { const ss=5;//定义常量 static public $cc; static function say() { echo "aaaa"; } } AA::$cc="hello"; echo AA::$cc; AA::say(); echo AA::SS; //final 关键字,用来修饰类,如果把一个类定义成final 就不能继承 $stu=new Student ("张三","男"); echo $stu->name; $stu->class="0001"; $stu->say(); /*$ren =new Ren("李四","男"); $ren->name ="张三";*/ /*$ren->name ="张三"; $ren->sex="女"; $ren->say(); $ren->setAge(20)*/ //var_dump($ren) ?>