Blogger Information
Blog 16
fans 0
comment 0
visits 11213
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
oop 编程的实例演绎
逃逃
Original
525 people have browsed it

oop 编程的实例演绎

[toc] //md 内容表

类的自动加载器:新建一个 autoload.php

  1. spl_autoload_register(function($className){
  2. // 先查看要加载的类
  3. printf('类名:%s<br>',$className);
  4. // 将类命名空间与类所在的路径进行一一映射
  5. // linux / window \
  6. // 解决在linux系统中命名空间失效的问题
  7. $file = __DIR__.'\\controller\\'.str_replace('\\',DIRECTORY_SEPARATOR,$className).'.php';
  8. // echo $file;
  9. if(!(is_file($file) && file_exists($file)))
  10. {
  11. throw new \Exception('文件名不合法或者不存在');
  12. }
  13. require $file;
  14. }

使用修饰符来对属性和方法进行封装:新建一个 Snacks.php

  1. abstract class Snacks //抽象类
  2. {
  3. private $name;
  4. protected $sort;
  5. public $pri;
  6. public function __construct($name='',$sort='水果')
  7. {
  8. $this->name = $name;
  9. $this->sort = $sort;
  10. $this->pri = $pri;
  11. }
  12. //抽象方法
  13. abstract function eat();
  14. // 普通方法
  15. public function Juicing()
  16. {
  17. echo '可以榨汁喝<br>';
  18. }
  19. public function getpri(){
  20. $this->pri = $this->pri + 2;
  21. return $this->pri ;
  22. }
  23. }

新建一个类文件 Apple.php

  1. class Apple extends Snacks //继承
  2. {
  3. public $pri = 3;
  4. public function getpri(){
  5. $this->pri = $this->pri + 2;
  6. return $this->pri ;
  7. }
  8. // 子类按照自己的需求去实现抽象方法, 如果抽象方法未全部实现 那么该类依然是一个抽象类
  9. public function eat()
  10. {
  11. echo "{$this->name}是{$this->sort} 可以直接吃";
  12. }
  13. public function Juicing()
  14. {
  15. echo "{$this->name} 可以榨汁喝<br>";
  16. }
  17. }

新建一个实例文件 demo1.php

  1. require 'autoload.php'; // 加载类文件 类的自动加载器
  2. $Apple = new Apple("苹果",'水果');
  3. $Apple->Juicing();
  4. $Apple->eat();
  5. echo '<hr>';

在实例文件 demo1.php 中输入

  1. $p = new Pear('香梨','水果');
  2. $p->eat();
  3. $p->Juicing();
  4. echo "苹果的价格: " . $Apple->getpri();
  5. echo "<br>";
  6. echo "香梨的价格: " . $p->getpri();
  7. //苹果和香梨的价格是都是5
  8. //echo Pear::$sort; //这里访问不到

新建一个类文件 Pear.php

  1. class Pear extends Apple
  2. {
  3. public function getSpri(){
  4. parent::getpri();// 这里调用了父类的方法.
  5. $this->pri = $this->pri + 2;
  6. return $this->pri ;
  7. }
  8. public function eat()
  9. {
  10. echo "{$this->name}是{$this->sort} 可以直接吃<br>";
  11. }
  12. public function Juicing()
  13. {
  14. echo "{$this->name} 可以榨汁喝<br>";
  15. }
  16. public function Fried()
  17. {
  18. echo "{$this->name} 可以裹上脆皮糊炸着吃<br>";
  19. }
  20. }
  • 文中 Snacks.php,Apple.php,Pear.php,是在一个文件夹 controller,demo1.php 和 autoload.php 是在一个文件夹,controller 文件夹,demo1.php 和 autoload.php 是平级
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post