<?php /** * Created by PhpStorm. * User: dllo * Date: 16/8/9 * Time: 上午10:07 */ header("Content-type:text/html;charset=utf-8"); // 具有相同特征和行为归为一类 class Person{ // 类中的常量 const x = 10; // 公开的 // 公开的属性可以在外界随意访问,或者修改 public $sex = "男"; // 受保护的 // 受保护的属性只能在类与子类中访问,在类的外部无法访问 protected $name = "kitty"; // 私有的 // 只能在本类中访问,外界都无法访问 private $hobby = "play"; public $age; function sayHi(){ echo "hi"; } // 构造函数 // 在类初始化的时候调用 function __construct($age) { $this -> age = $age; } //析构函数 // 在对象被内存回收时调用 function __destruct() { // TODO: Implement __destruct() method. } } // 子类继承于父类 class Student extends Person{ // 在外界想要访问被访问的属性,只能通过函数访问 function getName(){ echo $this -> name; } // 函数重载 // 子类中对父类的某一个方法有自己的实现方式,可以重写这个方法 function sayHi(){ // parent::访问父类中的方法 parent::sayHi(); echo "hello"; } } // 访问类中的常量 Person::x; $per = new Person(20); echo $per ->sex; $per -> sex = "女"; echo $per ->sex; //echo $per -> name; $stu = new Student(20); $stu -> getName(); $stu -> sayHi(); $per1 = new Person(20); echo $per1 -> age; $arr = [1,2]; // 抑止符 echo @$arr[2];
class.php
最新推荐文章于 2021-03-20 08:19:13 发布