1、父类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/5 0005 * Time: 12:11 */ class computer { protected $name; protected $cpu; protected $SATA; protected $power; protected $size; protected $bios; protected $Memory; public function movie(){ return 'look at movie'; } public function music(){ return 'licening to music'; } public function played_game($name,$cpu,$SATA,$power,$size,$bios,$Memory){ $msg[] = $this->name=$name; $msg[] = $this->cpu=$cpu; $msg[] = $this->SATA=$SATA; $msg[] = $this->power=$power; $msg[] = $this->size=$size; $msg[] = $this->Memory=$Memory; $msg[] =$this->bios=$bios; return implode(',',$msg).'以上是玩大型游戏的配置'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
2、子类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/5 0005 * Time: 12:26 */ //类的继承与重载 class extends_computer extends computer { //创建查询器查询 实现外部访问 public function __get($name) { $this->name; } //继承父类的公有或受保护的属性 使用属性不能继承 protected 受保护的 public 公有的 //重载方法玩played_game() public function played_game($name,$cpu,$SATA,$power,$size,$bios,$Memory) { // $msg[] = $this->name = $name; // // $msg[] = $this->cpu=$cpu; // $msg[] = $this->SATA=$SATA; // $msg[] = $this->power=$power; // $msg[] = $this->size=$size; // $msg[] = $this->Memory=$Memory; // $msg[] =$this->bios=$bios; //1.引用父类中的方法 return parent::played_game($name,$cpu,$SATA,$power,$size,$bios,$Memory); //2.重载父类中的方法 return '大吉大利,晚上吃鸡:想要吃鸡需要有以下的电脑配置'.implode(',',$msg); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
3、测试demo
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/5 0005 * Time: 12:26 */ //类的继承与重载 class extends_computer extends computer { //创建查询器查询 实现外部访问 public function __get($name) { $this->name; } //继承父类的公有或受保护的属性 使用属性不能继承 protected 受保护的 public 公有的 //重载方法玩played_game() public function played_game($name,$cpu,$SATA,$power,$size,$bios,$Memory) { // $msg[] = $this->name = $name; // // $msg[] = $this->cpu=$cpu; // $msg[] = $this->SATA=$SATA; // $msg[] = $this->power=$power; // $msg[] = $this->size=$size; // $msg[] = $this->Memory=$Memory; // $msg[] =$this->bios=$bios; //1.引用父类中的方法 return parent::played_game($name,$cpu,$SATA,$power,$size,$bios,$Memory); //2.重载父类中的方法 return '大吉大利,晚上吃鸡:想要吃鸡需要有以下的电脑配置'.implode(',',$msg); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
4、类的自动加载函数 spl_autoload_register()
<?php /* * 类的自动加载 * spl_autoload_register() * * * */ spl_autoload_register(function($classname){ //替换命名空间的中的反斜线进行转义 变成目录后再处理 $classname = str_replace('\\','/',$classname); //获取文件路径 $path = __DIR__.'/class/'.$classname.'.php'; //判断文件是不是个文件且 文件是否存在 if((is_file($path)) && file_exists($path)){ require $path; } }); //$test = new autoload1(); //echo $test->name; //$test = new autoload2(); //echo $test->name; ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例