Blogger Information
Blog 16
fans 7
comment 1
visits 11366
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月3日-面向对象
Eric
Original
686 people have browsed it

一、OOP编程基本流程

  1. // 1. 创建类
  2. class Demo1
  3. {
  4. // 2. 添加类成员
  5. // 在类中的变量和函数,和程序中的变量与函数有区别, 添加了访问限制符
  6. // 变量=>属性, 函数=>方法, 访问限制符 成员
  7. // 创建属性
  8. public $site = 'php中文网';
  9. // 创建方法: 输出当前类的属性$site
  10. public function getSite()
  11. {
  12. // 第一步:类的实例化
  13. $obj = new Demo1;
  14. // 第二步: 返回类属性
  15. return $obj->site . ' 欢迎您~~';
  16. }
  17. }
  18. // 3. 访问类成员
  19. $obj = new Demo1;
  20. // 访问属性: 对象成员访问符: ->
  21. echo $obj->site . '<br>';
  22. echo $obj->getSite();

二、类与实例的引用

self: 引用当前类
$this: 引用当前类的实例/对象

  1. class Demo
  2. {
  3. private $name;
  4. private $age;
  5. public function __construct($name, $age)
  6. {
  7. //$obj = new self();
  8. $this->name = $name;
  9. $this->age = $age;
  10. }
  11. public function getInfo()
  12. {
  13. return $this->name . '--' . $this->age;
  14. }
  15. }
  16. $obj = new Demo('唐僧', 30);
  17. echo $obj->getInfo(); //唐僧--30

三、构造方法

__construct(): 类实例化自动调用,完成对象的初始化
new: 当用new调用一个类名称函数时会触发类中的构造方法

  1. <?php
  2. class Demo
  3. {
  4. private $name;
  5. private $age;
  6. //当类被实例化,构造方法就会自动完成方法内的所有操作
  7. public function __construct($name, $age)
  8. {
  9. $this->name = $name;
  10. $this->age = $age;
  11. $this->getInfo();
  12. }
  13. public function getInfo()
  14. {
  15. return $this->name.'--'.$this->age;
  16. }
  17. }
  18. echo new Demo('唐僧', 30); //唐僧--30

四、类成员的访问控制

public: 本类, 类外, 子类均可访问
private: 仅本类可访问
protected: 本类,子类可访问,类外不可见

  1. class Demo{
  2. private $name;
  3. private $age;
  4. public function __construct($name, $age)
  5. {
  6. $this->name = $name;
  7. $this->age = $age;
  8. }
  9. //使用成员方法来访问 私有化成员属性
  10. public function getInfo(){
  11. return $this->name.'--'.$this->age;
  12. }
  13. // 魔术方法: __get($name), 属性重载
  14. public function __get($name)
  15. {
  16. // 仅允许用户名是'admin'的用户访问,其它访问返回: 无权访问
  17. $username = $_GET['username'] ?? '';
  18. if (isset($username) && $username === 'admin') {
  19. return isset($this->$name) ? $this->$name : '属性未定义';
  20. } else {
  21. return '无权访问';
  22. }
  23. }
  24. }
  25. $obj = new Demo('唐僧',30);
  26. echo $obj->getInfo(); //唐僧--30
  27. echo $obj->sex; //属性未定义

五、类的继承

protected 受保护成员,可以子类中访问
extends 设置子类继承的父类
parent 子类中引用父类成员代码

  1. class Demo{
  2. public $name;
  3. public $age;
  4. public function __construct($name, $age)
  5. {
  6. $this->name = $name;
  7. $this->age = $age;
  8. }
  9. }
  10. class Child extends Demo {
  11. public $sex;
  12. /**
  13. * Child constructor.
  14. * @param $sex
  15. */
  16. public function __construct($name, $age, $sex)
  17. {
  18. parent::__construct($name,$age);
  19. $this->sex = $sex;
  20. }
  21. public function getInfo(){
  22. return $this->name.'--'.$this->age.'--'.$this->sex;
  23. }
  24. }
  25. $obj = new Child('唐僧',30);
  26. echo $obj->getInfo(); //唐僧--30--男

六、接trait技术

trait 声明trait类,实现类的多继承与方法复用
use 在类中插入trait类代码

  1. trait Common{
  2. public function getInfo(){
  3. return $this->name.'--'.$this->age.'--'.$this->sex;
  4. }
  5. }
  6. class Demo{
  7. public $name;
  8. public $age;
  9. public function __construct($name, $age)
  10. {
  11. $this->name = $name;
  12. $this->age = $age;
  13. }
  14. }
  15. class Child extends Demo {
  16. //使用trait技术,实现了Child类继承多累的需求
  17. use Common;
  18. public $sex;
  19. /**
  20. * Child constructor.
  21. * @param $sex
  22. */
  23. public function __construct($name, $age, $sex)
  24. {
  25. parent::__construct($name,$age);
  26. $this->sex = $sex;
  27. }
  28. }
  29. $obj = new Child('唐僧',30,'男');
  30. echo $obj->getInfo(); //唐僧--30--男

七、接口interface

interface 定义接口
implements 实现接口

  1. interface iCommon{
  2. public function hello();
  3. public function say();
  4. }
  5. class Demo implements iCommon {
  6. public $name;
  7. public $age;
  8. public function __construct($name, $age)
  9. {
  10. $this->name = $name;
  11. $this->age = $age;
  12. }
  13. function hello()
  14. {
  15. return 'hello '.$this->name;
  16. }
  17. function say()
  18. {
  19. return 'say '.$this->name;
  20. }
  21. }
  22. $obj = new Demo('唐僧',30);
  23. echo $obj->hello(); //hello 唐僧
  24. echo $obj->say(); //say 唐僧

八、抽象类abstract

  1. abstract class Common{
  2. abstract function hello();
  3. public function say(){
  4. return 'say goodbye! '.$this->name;
  5. }
  6. }
  7. class Demo extends Common {
  8. public $name;
  9. public $age;
  10. public function __construct($name, $age)
  11. {
  12. $this->name = $name;
  13. $this->age = $age;
  14. }
  15. function hello()
  16. {
  17. return 'hello '.$this->name;
  18. }
  19. }
  20. $obj = new Demo('唐僧',30);
  21. echo $obj->hello(); //hello 唐僧
  22. echo $obj->say(); //say goodbye! 唐僧

手写:


THE END !

Correcting teacher:天蓬老师天蓬老师

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!