批改状态:合格
老师批语:很多地方,还是要多多理解
类是对某个对象的定义。它包含有关对象动作方式的信息,包括它的名称、方法、属性和事件。实际上它本身并不是对象,因为它不存在于内存中。当引用类的代码运行时,类的一个新的实例,即对象,就在内存中创建了。虽然只有一个类,但能从这个类在内存中创建多个相同类型的对象。类是通过class关键字来定义的。
基本语法:
class 类名{1.属性2.方法}
注意事项:
1.定义一个类(只能用class定义)
举个例子:
定义一个汽车类,属性有车的颜色和价格
class Car{public $color; //定义颜色public $price; //定义价格}
类的实例化又叫创建一个对象或者实例化一个对象或者把类实例化。
简单的来举个例子:
我们定义一个人汽车,并把这个类实例化。
class Car{public $color = '蓝色'; //定义汽车颜色public $price = 1960000; //定义汽车价格public $model = 'BMW-X5M';}$car = new Car; // 实例化类echo '颜色:'.$car->color;echo '价格:'.$car->price;echo '型号:'.$car->model;

对属性或方法的访问控制,是通过在前面添加关键字 public、protected 或 private 来实现的。
类成员都必须使用关键字public、protected 或 private 进行定义
示例:
<?phpclass Car{public $color = '蓝色'; //定义汽车颜色公用属性protected $price = 1960000; //定义汽车价格私有属性private $model = 'BMW-X5M'; //隐私属性public function mycar(){return "我的车是:{$this->model}";}}class Rob extends Car{public function mycar1(){return "我的车是:{$this->model},车价格是:{$this->price},颜色是:{$this->color}";}}$car = new Rob; // 实例化类echo $car->mycar();echo "<hr>";echo $car->mycar1();echo "<hr>";echo "我的车颜色是:".$car->color;echo "<hr>";echo "我的车是:{$car->model},车价格是:{$car->price},颜色是:{$car->color}";

在类中我们可以定义属性及方法,可以在类中扩展出子类,使类的功能更加强大。
在下面示例中我使用了类的几种扩展加深对类的学习。
示例:
<?phpclass User{//公用属性 public 在类和类中及外部都可与调用//受保护的属性 protected 在类和子类用可与调用//私有属性 private 在类中可以调用子类和外部不能调用private $user ='曹操';public $age = 66;public $nation ='魏国';public static $mobile='13999999999'; //静态公用属性public static $like = '貂蝉';protected $mobilep ='13666666666';public function attribute(){return "人物属性:姓名:{$this->user}|年龄:{$this->age}|国家:{$this->nation}|电话:".self::$mobile;//返回人物属性,使用了self调用静态属性}public static function attribute1(){$mo = new User;return "人物属性:姓名:".self::$like."|电话:{$mo->mobilep}";//静态方法使用$this只能调用静态属性,在方法中实例化类才能调用类属性}}class Rob extends User //子类扩展{public $user = '刘备';private $agec = 58;private $mobilec ='13000000000';private $nationc ='蜀国';public function attribute2(){return "人物属性:姓名:{$this->user}|年龄:{$this->agec}|国家:{$this->nationc}|电话:{$this->mobilec}";}public function attribute3(Closure $userattr,String $username){return "人物属性:姓名:".$username."|".$userattr()."|喜欢:".self::$like;//调用外部属性和方法,静态属性不可以被重定}public function attribute4(){$user = '董卓';$age = 72;$mobile = 13111111112;$nation = '东汉';return "人物属性:姓名:{$user}|年龄:{$age}|国家:{$nation}|电话:{$mobile}|喜欢:".self::$like;}public function attribute5(Int $num){switch ($num){case 1:echo $this->attribute();break;case 2:echo $this->attribute1();break;case 3:echo $this->attribute2();break;default:echo $this->attribute4();}}}$username = '董卓';$userattr = function(){$age= 75;$nation ='魏国';$mobile ='13333333333';return "年龄:".$age."|国家:".$nation."|电话:".$mobile;};$user = new Rob; //调用子类包含父类的属性和方法echo $user->attribute();echo '<hr>';echo $user->attribute1();echo '<hr>';echo $user->attribute2();echo '<hr>';echo $user->attribute3($userattr,$username);echo '<hr>';$num =rand(1,4);echo $user->attribute5($num);
示例图
对类的基础使用有所了解,在对属性也基本认识。对类的扩展和使用基本学会。
但对静态的处理及类以及外部传参调用,还需要多学习
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号