search
HomeBackend DevelopmentPHP TutorialPHP introductory tutorial: Analysis of object-oriented features (inheritance, polymorphism, interfaces, abstract classes, abstract methods, etc.)

The examples in this article describe the object-oriented features of PHP. Share it with everyone for your reference, the details are as follows:

Demo1.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  //创建一个电脑类
  class Computer {
    //什么叫做类内,就是创建类的花括号内的范围叫做类内,其他地方则类外。
    //public 是对字段的公有化,这个字段类外即可访问,赋值和取值
    public $_name = &#39;联想&#39;;
  }
  $computer = new Computer();
  $computer -> _name = &#39;Dell&#39;;
  echo $computer->_name;
?>

Demo2.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    //private 是私有化,即对字段进行封装的操作,类外无法访问,取值和赋值都不能操作
    private $_name = &#39;联想&#39;;
  }
  $computer = new Computer();
  echo $computer->_name;
?>

Demo3.php

<?php
  header(&#39;Content-Type:text/html; charset=utf-8;&#39;);
  class Computer {
    private $_name = &#39;联想&#39;;
    //这个时候我采用一个公共对外的方法来访问私有字段
    //因为私有字段只能在类内访问,而对外的公共方法是类内的。
    //更而公共方法又是公共的,所以类外又可访问。
    public function _run(){
      //字段在类内调用的时候必须是类 -> 字段,而$_name只是一个普通变量而已。
      //字段在类外调用的方法是对象 -> 字段,而类内就必须使用 Computer -> _name
      //但是在本类中,可以使用一个关键字来代替字来代替 Computer ,那就是 $this
      echo $this ->_name;
    }
  }
  $computer = new Computer();
  $computer -> _run();
?>

Demo4.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    private $name;
    private $model;
    private $cpu;
    private $keyboard;
    private $show;
    private $zb;
    //必须写个对外的入口,才可以取到
    public function getName() {
      return $this->name;
    }
    //必须写一个对内的入口,对私有字段进行赋值
    public function setName($name) {
      //这里的 $name 只是一个变量而已,参数而已
      //$this->name 才是类的字段
      $this->name = $name;
    }
  }
  $computer = new Computer ();
  echo $computer->getName();
  $computer->setName(&#39;Dell&#39;);
  echo $computer->getName();
?>

Demo5.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    private $_name;
    private $_model;
    private $_cpu;
    //当类外的对象直接调用私有字段时,会跟着去检查是否有拦截器,
    //如果直接对 $_name 进行赋值,那么__set 方法就会拦截住,就不会报错了。
    //采用拦截器进行赋值和取值
    //赋值
    private function __set($_key,$_value){
      //采用$_key = &#39;_name&#39;,那么 $_value = &#39;联想&#39;;
      //$this ->_name = &#39;联想&#39;;
      $this ->$_key = $_value;
    }
    //取值
    private function __get($_key){
      return $this -> $_key;
      //如果 $_key = &#39;_name&#39; 那么 $this -> _name;
      //如果 $_key = &#39;_cpu&#39; 那么 $this -> _cpu;
      //如果 $_key = &#39;_model&#39; 那么 $this -> _model;
    }
  }
  $computer = new Computer ();
  $computer->_name = &#39;联想&#39;;
  $computer->_cpu = &#39;四核&#39;;
  $computer->_model = &#39;i7&#39;;
  echo $computer->_name;
  echo $computer->_cpu;
  echo $computer->_model;
?>

Demo6.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    private $_name;
    private $_model;
    private $_cpu;
    //__set 和 __get 方法私有了,还是可以执行,是因为
    //因为目前程序的指针已经在类内了。而类内可以执行封装的方法
    //类内执行私有方法,不会出现任何错误。
    //它只需要间接的拦截就可以了。拦截是在内类执行的。
    //说白了,__set() 和 __get() 是 PHP 内置的方法,具有一定的特殊性
    private function __set($_key, $_value) {
      $this->$_key = $_value;
    }
    private function __get($_key) {
      return $this->$_key;
    }
  }
  $computer = new Computer ();
  $computer->_name = &#39;联想&#39;;
  $computer->_cpu = &#39;四核&#39;;
  $computer->_model = &#39;i7&#39;;
  echo $computer->_name;
  echo $computer->_cpu;
  echo $computer->_model;
?>

Demo7.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    const NAME = &#39;DELL&#39;;
  }
  //常量的输出方法 类::常量
  echo Computer::NAME;    //DELL
?>

Demo8.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public $_count = 0;
    public function _add(){
      $this -> _count++;  //$_count = $_count+1 $_count++
    }
  }
  //做一个累计的效果
  $computer1 = new Computer();
  $computer1 ->_add();
  $computer1 ->_add();
  $computer1 ->_add();
  echo $computer1 -> _count;
  echo &#39;<br />&#39;;
  $computer2 = new Computer();
  $computer2 ->_add();
  $computer2 ->_add();
  $computer2 ->_add();
  echo $computer2 -> _count;
?>

Demo9.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public static $_count = 0;
    public function _add(){
      //如果是静态成员字段,那么就应该用 self 来调用,而不是 $this
      self::$_count++;
    }
  }
  //做一个累计的效果
  $computer1 = new Computer();
  $computer1 ->_add();
  echo Computer::$_count;
  $computer1 ->_add();
  echo Computer::$_count;
  $computer1 ->_add();
  echo Computer::$_count;
  echo &#39;<br />&#39;;
  $computer2 = new Computer();
  $computer2 ->_add();
  echo Computer::$_count;
  $computer2 ->_add();
  echo Computer::$_count;
  $computer2 ->_add();
  echo Computer::$_count;
?>

Demo10.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public static $_count = 0;
    public static function _add(){
      self::$_count++;
    }
  }
  Computer::_add();
  Computer::_add();
  Computer::_add();
  echo Computer::$_count;
?>

Demo11.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
  }
  $computer = new Computer();
  echo $computer instanceof Computer;
?>

Demo12.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //这是父类,电脑类
  class Computer {
    public $_name = &#39;联想&#39;;
    public function _run(){
      echo &#39;联想在运行!&#39;;
    }
  }
  //子类,笔记本电脑类
  class NoteComputer extends Computer {
  }
  $noteComputer = new NoteComputer();
  echo $noteComputer -> _name;
  $noteComputer -> _run();
?>

Demo13.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public $_name = &#39;联想&#39;;
    public function _run(){
      echo &#39;联想在运行!&#39;;
    }
  }
  class NoteComputer extends Computer {
    //我不需要父类的字段和方法,那么可以采用重写的方法覆盖掉父类的字段和方法
    public $_name = &#39;Dell&#39;;
    public function _run(){
      echo &#39;Dell在运行!&#39;;
    }
  }
  $noteComputer = new NoteComputer();
  echo $noteComputer -> _name;
  $noteComputer -> _run();
?>

Demo14.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    //私有化,但是无法被子类继承,这个时候就应该用受保护的修饰符来封装
    protected $_name = &#39;联想&#39;;
    protected function _run(){
      return &#39;联想在运行!&#39;;
    }
  }
  class NoteComputer extends Computer {
    public function getTop() {
      echo $this->_name;
      echo $this->_run();
    }
  }
  $noteComputer = new NoteComputer();
  $noteComputer -> getTop();
?>

Demo15.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  class Computer {
    public $_name = &#39;联想&#39;;
    public function _run(){
      return &#39;联想在运行!&#39;;
    }
  }
  class NoteComputer extends Computer {
    //我子类已经覆盖了父类的字段和方法,
    //但是我又要调用父类的字段和方法,那怎么办呢?
    public $_name = &#39;Dell&#39;;
    public function _run(){
      echo &#39;Dell在运行!&#39;;
      echo parent :: _run();
    }
  }
  $noteComputer = new NoteComputer();
  echo $noteComputer -> _name;
  $noteComputer -> _run();
  //DellDell在运行!联想在运行!
?>

Demo16.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //final 如果加在类前面,表示这个类不能被继承
// final class Computer {
// }
  class Computer {
    //final 如果加在方法前面,表示不能够重写些方法
    final public function _run(){
    }
  }
  class NoteComputer extends Computer {
    public function _run(){
    }
  }
  $noteComputer = new NoteComputer();
?>

Demo17.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //创建一个抽象类,只要在 class 前面加上 abstract 就是抽象类了
  //抽象类不能够被实例化,就是创建对象
  //只在类里面有一个抽象方法,那么这个类必须是抽象类,类前面必须加上 abstract
  abstract class Computer {
    public $_name = &#39;联想&#39;;
    //抽象类里创建一个抽象方法
    //抽象方法不能够实现方法体的内容
    abstract public function _run();
    //我在抽象类里能否创建一个普通方法
    public function _run2(){
      echo &#39;我是父类的普通方法&#39;;
    }
  }
  //类不能够实现多继承,只支持单继承。
  //抽象类是给子类用来继承的,实现一种规范和资源的共享
  class NoteComputer extends Computer {
    //抽象类的抽象方法,子类必须重写,不然会报错。
    //抽象类里的普通方法不需要重写,子类会直接继承下来
    public function _run(){
      echo &#39;我是子类的方法&#39;;
    }
  }
  $noteComputer = new NoteComputer();
  $noteComputer -> _run();
  $noteComputer -> _run2();
  echo $noteComputer -> _name;
?>

Demo18.php

<?php
  /*
   * 到底应该用抽象类还是接口呢
   * 如果你要继承多个接口的方法规范,那么就用接口好了。
   * 如果你要共享一个方法体内容,那么就用抽象类。
   * */
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //创建一个接口
  //接口也不能被实例化
  //接口是为了规范实现它的子类,以达到统一的目的。也可以共享数据
  interface Computer {
    //成员字段必须是变量
    const NAME = &#39;成员 &#39;;
    //接口里的所有方法都是抽象方法,不能够写方法体
    //并且接口的抽象方法不需要写 abstract
    public function _run();
    public function _run2();
  }
  interface Computer2 {
    public function _run3();
  }
  //子类继承接口的说法,叫做实现,接口可以多实现
  class NoteComputer implements Computer,Computer2 {
    public function _run() {
      echo &#39;我重写了run&#39;;
    }
    public function _run3() {
      echo &#39;我重写了run3&#39;;
    }
    public function _run2() {
      echo &#39;我重写了run2&#39;;
    }
  }
  $noteComputer = new NoteComputer();
  $noteComputer -> _run();
  $noteComputer -> _run2();
  $noteComputer -> _run3();
  echo NoteComputer::NAME;
  //接口 :: 常量
  //echo Computer::NAME;
?>

Demo19.php

<?php
  header ( &#39;Content-Type:text/html; charset=utf-8;&#39; );
  //什么叫做多态,字面意思,多种形态
  //一个动作由不同的人去执行,而产生不同的效果或者效果,即为多态。
  //一个人通过不同的状态去执行同一种动作,形成不同的效果,也可以称作为多态。
  //园丁    剪    修理花草
  //理发师  剪    理发
  //总裁    剪    裁员
  //人   笔记本   运行 win7开机了
  //人   台式机   运行 xp开机了
  //创建一个接口,来规范运行的方法
  interface Computer {
    public function version(); //这个方法表示采用什么电脑
    public function work();   //这台电脑是怎么运行的
  }
  //创建一个笔记本的类实现接口
  class NoteComputer implements Computer {
    public function version() {
      echo &#39;笔记本&#39;;
    }
    public function work() {
      echo &#39;可以便携式运行 win7&#39;;
    }
  }
  //创建一个台式机的类实现接口
  class DesktopComputer implements Computer {
    public function version() {
      echo &#39;台式机&#39;;
    }
    public function work() {
      echo &#39;在工作站运行 XP&#39;;
    }
  }
  //创建一个用户
  class Person {
    //创建一个方法来接受电脑(笔记本电脑,也可以是台式电脑)
    //怎么接受,将他们的对象传进来就 OK 啦。
    public function _run($type) {
      echo &#39;这个人的&#39;;
      $type -> version();
      $type ->work();
    }
  }
  //多态的原理,就是类都写好了,不要去修改它,只要在类外的调用参数的更改
  //而最后的结果也会得到更改,那么这个就是多态。
  //有一个接口,两个类,一个是笔记本的类,一个是台式机的类
  //创建了笔记本
  $noteComputer = new NoteComputer();
  //创建台式机
  $desktopComputer = new DesktopComputer();
  //创建一个人
  $person = new Person();
  //使用电脑
  $person -> _run($noteComputer); //这种传递,叫做对象引用的传递
?>

I hope this article will be helpful to everyone in PHP programming.

For more PHP introductory tutorials on object-oriented feature analysis (inheritance, polymorphism, interfaces, abstract classes, abstract methods, etc.), please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何使用Go语言实现面向对象的事件驱动编程如何使用Go语言实现面向对象的事件驱动编程Jul 20, 2023 pm 10:36 PM

如何使用Go语言实现面向对象的事件驱动编程引言:面向对象的编程范式被广泛应用于软件开发中,而事件驱动编程是一种常见的编程模式,它通过事件的触发和处理来实现程序的流程控制。本文将介绍如何使用Go语言实现面向对象的事件驱动编程,并提供代码示例。一、事件驱动编程的概念事件驱动编程是一种基于事件和消息的编程模式,它将程序的流程控制转移到事件的触发和处理上。在事件驱动

解析PHP面向对象编程中的享元模式解析PHP面向对象编程中的享元模式Aug 14, 2023 pm 05:25 PM

解析PHP面向对象编程中的享元模式在面向对象编程中,设计模式是一种常用的软件设计方法,它可以提高代码的可读性、可维护性和可扩展性。享元模式(Flyweightpattern)是设计模式中的一种,它通过共享对象来降低内存的开销。本文将探讨如何在PHP中使用享元模式来提高程序性能。什么是享元模式?享元模式是一种结构型设计模式,它的目的是在不同对象之间共享相同的

go语言是面向对象的吗go语言是面向对象的吗Mar 15, 2021 am 11:51 AM

go语言既不是面向对象,也不是面向过程,因为Golang并没有明显的倾向,而是更倾向于让编程者去考虑该怎么去用它,也许它的特色就是灵活,编程者可以用它实现面向对象,但它本身不支持面向对象的语义。

python是面向对象还是面向过程python是面向对象还是面向过程Jan 05, 2023 pm 04:54 PM

python是面向对象的。Python语言在设计之初,就定位为一门面向对象的编程语言,“Python中一切皆对象”就是对Pytho 这门编程语言的完美诠释。类和对象是Python的重要特征,相比其它面向对象语言,Python很容易就可以创建出一个类和对象;同时,Python也支持面向对象的三大特征:封装、继承和多态。

PHP面向对象编程入门指南PHP面向对象编程入门指南Jun 11, 2023 am 09:45 AM

PHP作为一种广泛使用的编程语言,已成为构建动态网站和网络应用程序的首选语言之一。其中,面向对象编程(OOP)的概念和技术越来越受到开发者的欢迎和推崇。本篇文章将为读者提供PHP面向对象编程的入门指南,介绍OOP的基本概念,语法和应用。什么是面向对象编程(OOP)?面向对象编程(Object-OrientedProgramming,简称OOP),是一种编程

如何使用Go语言实现面向对象的数据库访问如何使用Go语言实现面向对象的数据库访问Jul 25, 2023 pm 01:22 PM

如何使用Go语言实现面向对象的数据库访问引言:随着互联网的发展,大量的数据需要被存储和访问,数据库成为了现代应用开发中的重要组成部分。而作为一门现代化、高效性能的编程语言,Go语言很适合用来处理数据库操作。而本文将重点讨论如何使用Go语言实现面向对象的数据库访问。一、数据库访问的基本概念在开始讨论如何使用Go语言实现面向对象的数据库访问之前,我们先来了解一下

Python中的面向对象编程Python中的面向对象编程Jun 10, 2023 pm 05:19 PM

Python作为一种高级编程语言,在众多编程语言中占有举足轻重的地位。它的语法简单易学,拥有各种强大的编程库,被广泛应用于数据处理、机器学习、网络编程等领域。而其中最重要的一点便是Python完美支持面向对象编程,本文将重点阐述Python中的面向对象编程。一、面向对象编程的基本概念在面向对象的编程语言中,数据和方法被封装在对象的内部。这使得对象能够独立地进

面向对象是啥意思面向对象是啥意思Jul 17, 2023 pm 02:03 PM

面向对象是软件开发方法,一种编程范式。是一种将面向对象的思想应用于软件开发过程并指导开发活动的系统方法。这是一种基于“对象”概念的方法论。对象是由数据和允许的操作组成的包,它与目标实体有直接的对应关系。对象类定义了一组具有类似属性的对象。面向对象是基于对象的概念,以对象为中心,以类和继承为构建机制,认识、理解和描绘客观世界,设计和构建相应的软件系统。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment