Correction status:qualified
Teacher's comments:命名空间的概念非常重要, 以后我们的所有代码全部要放在空间中了
<?php /** * Created by PHPSTORM. * User: ChenGuo * Date: 2019/8/4 * Time: 19:42 */ namespace model; echo "类与对象的理解"."<br>"; echo "万物皆对象,我们把对象总结成一个类"."<br>"; echo "比如pc,pc有价格有*** 这是类属性,能打字能玩游戏 这是类方法"."<br>"; echo "<hr>"; class pc{ //类属性 public $price = 10000; public $name = "联想"; public $type = "Y470"; public function getInfo(){ echo "我今天逛街,买了一台电脑,花了我".$this->price."元,我好心痛啊,他是".$this->name."牌子的".$this->type."型号"; } public function playGame($a){ return "我能用他打游戏,比如".$a; } public function count($a,$b){ return "我能用他计算,比如".$a."+".$b."=".($a+$b); } public function __destruct() { // TODO: Implement __destruct() method. echo "<h1>对象已经被摧毁咯</h1>"; } } $pc = new pc(); echo "我今天逛街,买了一台电脑,花了我".$pc->price."元,我好心痛啊,他是".$pc->name."牌子的".$pc->type."型号"."<br>"; echo "用get_class_vars获取所有类变量:"."<br>"; echo "<pre>".print_r($pcpro = get_class_vars(pc::class),true)."<br>"; //总结:执行顺序:先执行方法 echo "我用this方法去获取类变量:::".$pc->getInfo()."<br>"; echo $pc->playGame("LOL")."<br>"; echo $pc->count(1,2)."<br>";
类的封装与集成
<?php /** * Created by PHPSTORM. * User: ChenGuo * Date: 2019/8/4 * Time: 20:00 */ namespace model; //命名空间主要是为了解决自己写的类、常量、函数和php内部名称冲突,也提高了代码的可读性 class animal{ public $price; public $name; public $type; public $master ; public function cry(){ return "大部分动物会交"; } } class dog extends animal { public function cry() { //return parent::cry(); // TODO: Change the autogenerated stub return "我会汪汪汪"; } } $dog = new dog(); $dog->price = "1"; $dog->master = "PHP"; echo "我的主人是".$dog->master.",我是主人花了".$dog->price."快钱买来的<br>"; echo ($dog->cry());
点击 "运行实例" 按钮查看在线实例