search
HomeBackend DevelopmentPHP TutorialPHP object-oriented simple summary
PHP object-oriented simple summaryMay 29, 2020 am 10:55 AM
object-oriented

Basic knowledge

Class: A collection of classes with the same attributes or methods. For example, Chevrolet cars are a Chevrolet car category, Mercedes-Benz cars are a Mercedes-Benz car category, and BMW cars are a BMW car category, and these three categories are subcategories of the main car category.
Object: A specific implementation of the class. For example, the BMW Q5 is a specific implementation of the BMW car class. Objects are stored in memory. Let's take a look at the allocation of objects in memory.

PHP object-oriented simple summary

Using object-oriented means to use the above two knowledge points flexibly. Let us create and use classes and objects

<?php
    /**
    *新建一个类
    */
    class newClass{        public $a;        public $b;        public function funA(){            echo "I am function A";
        }        public function funB(){            echo "I am function B";
        }
    }    /**
    *使用类创建对象
    */
    $opt=new newClass();
    $opt->a="opt";//将opt对象中的a属性赋值为opt
    $opt->funA();//打印出"I am function A"?>


Modifiers: When defining classes and attributes in the class, we can use the following three modifiers. If not added, the default is public
public: Maximum permissions
protected: The scope of permissions is within itself and its subclasses
private: The scope of permissions is only within itself
Constructor and destructor: When we instantiate a class to create an object, we often need to initialize the object. At this time, we need to define a constructor method in the class. When we are done using the object, we need to release the object to reduce memory usage. At this time we need to use the destructor method.
In php we use __construct() method and __destruct(), the following code

<?php
class newClass{
    public $a;
    public $b;
    public function __construct($a,$b){
        $this->a=$a;
        $this->b=$b;
        echo "我是构造函数";
    }
    public function __destruct(){
        echo "我是析构函数";
    }
}
?>

Encapsulation

When we When developing some important program modules, we often do not want others to easily access the data of these program modules, so these data need to be encapsulated. At this time we need to perform data access control, often using private keyword to encapsulate these properties and methods.
As follows:

<?php
class privateClass
{
    private $a;
    private $b;
    private function privateFun()
    {
        echo "我是封装的方法";
    }
} ?>

In this way, when we create an object, we cannot call private properties and methods. But we can access these private properties and methods by using magic methods.
Use of __set() and __get()
Through the __set() method we can directly set the member attribute values ​​​​through the object
Through the __get() method we can directly obtain the member attribute values ​​​​through the object

<?php
class setClass
{        
     private $a;        
    private $b="ww";        
    public function __set($oldValue,$newvalue){ 
        $this->$oldValue=$newvalue;
    }        
    public function __get($newvalue){            
        return $newvalue;
    }
}
    $opt= new setClass();
    $opt->a="sss";//直接设置私有属性值
    echo $opt->b;//直接读取私有属性值
?>

Use of __isset() and __unset()
Directly check whether the private attributes in the object exist through __isset()
Directly delete the private attributes in the object through __unset()

<?php
class issetClass
{
    private $a;
    private $b = &#39;www&#39;;
    function __isset($privateName)
    {
        if (isset($privateName)) {
            return 1;
        } else {
            return 0;
        }
    }
    function __unset($privateName)
    {
        echo "销毁" . $privateName;
    }
}
$opt = new issetClass();
echo isset($opt->$b);
var_dump(unset($opt->$b));?>

Inheritance

When we write multiple classes, often multiple classes have the same properties and methods. In order to simplify the code we introduce inheritance The concept of subclasses can inherit some attributes and methods of the parent class, reducing redundant code writing.

How to write inheritance classes
We use the keyword extends to write subclasses

<?php
    class parentClass{
    }    
    class childClass extends parentClass{
    }    
?>

As above, childClass is a subclass of parentClass. PHP only supports single inheritance, that is, there is only one subclass. Parent class, but a parent class can have multiple subclasses.

Override the parent class method
When the method in the parent class cannot satisfy the use of the subclass, we can override the parent class method. But when we want to use the method of the parent class in the subclass, we can use the following syntax: parent:: method name. Several important keywords

3.1 final

final can modify the class With methods, member attributes cannot be modified;
Classes modified by final cannot be inherited, and methods modified with final cannot be overridden in subclasses

3.2 static

Static can modify member properties and member methods, but cannot modify classes;
Members and methods modified with static can be used directly through the class, using the following syntax: class name::\$property name or method name();
When used in a class, use the following syntax: self::\$ attribute name or method name.

3.3 const

Use const to declare constants in a class instead of define( );
For example, const TT=90, when using a constant, use the following syntax directly self::constant name

3.4 instanceof

Use this keyword to detect whether an instance is Is an instance of a class.

3.5 trait

php can only perform single inheritance, but it also provides an alternative way to reuse code and solve the problem of single inheritance.
As follows

<?php

trait testA
{
    function a()
    {
    }
}

trait testB
{
    function b()
    {
    }
}

class testC
{
    use testA, testB;

    function c()
    {
    }
} ?>

Abstract technology

Methods and classes modified with the abstract keyword are called abstract methods or abstract classes .
Declaring abstract classes and abstract methods

<?php
abstract class testClass
{
$a;
$b;
    abstract function testFun();
    abstract function testFun1();
    public function optFun()
    {
        echo "抽象类中的抽象方法";
    }//抽象类可以有非抽象方法
}
class childClass extends testClass
{
    function testFun()
    {
        echo "子类中实现抽象方法";
    }
    function testFun1()
    {
        echo "子类实现抽象方法";
    }
}
abstract child1Class extends testClass
{
}//子类是抽象方法可以不实现父类的抽象方法?>

Abstract technology provides a specification for the declaration of subclasses, limiting the instantiation of the class (abstract classes cannot be instantiated).

Interface technology

Interface is a special abstract class. Only abstract classes and constants can be declared in the interface
Declaration of interface usage interface, implement the interface using implements, the modifier can only be the default public;
A subclass can inherit multiple interfaces and inherit a parent class at the same time

<?php
interface newInterface
{
    const V = 12;
    abstract function fun1();
    abstract function fun2();
}
interface newInterface1
{
    abstract function fun3();
    abstract function fun4();
}
class parentClass
{
    function fun5()
    {
        echo "fun5";
    }
}
class optClass extends parentClass implements newInterface, newINterface1
{
    function fun1()
    {
        echo "fun1";
    }
    function fun2()
    {
        echo "fun2";
    }
    function fun3()
    {
        echo "fun3";
    }
    function fun4()
    {
        echo "fun4";
    }
} ?>

Polymorphism

Polymorphic implementation in PHP requires a class to be inherited through multiple subclasses. If a class method is rewritten in multiple subclasses and implements different functions, we call it polymorphism.

Recommended tutorial: "PHP Tutorial"



The above is the detailed content of PHP object-oriented simple summary. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
如何使用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语言实现面向对象的数据库访问之前,我们先来了解一下

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

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

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

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

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
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft