A brief discussion on object-oriented programming in PHP
The content of this article is about a brief discussion of PHP object-oriented programming, which has certain reference value. Friends in need can refer to it
1. Basic practice of PHP object-oriented programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
2. Advanced PHP object-oriented programming practice
Knowledge points: class inheritance, method rewriting, access control, static keyword, final key Words, data access, interfaces, polymorphism, abstract classes
2.1. Class inheritance: extends keyword
For example: an operator, a host, they have common human behavior methods, But they all have their own different specialties. Therefore, when programming, you need to create a parent class for them and inherit it;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
2.2. Method rewriting: subclasses rewrite the methods of the parent class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
2.3. Access control: public, protected , private keyword
public: Define public member attributes or methods, can be used anywhere
protected: Define protected member attributes or methods, only allowed to be used by the class itself or subclasses
private: Definition Private member attributes or methods are only allowed to be used by the class itself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
2.4, static (static) keyword
1), static attributes are used to save the public data of the class;
2), Only static properties or methods can be accessed in static methods, and the $this pseudo variable cannot be used;
3). Static members can be accessed and used without instantiating the object through the new keyword;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
2.5. final keyword: member methods are not allowed to be overridden or inherited.
Example: 1. For the method "Eat" of the parent class, we do not want the subclass to rewrite it; 2. For the class "Athletes", we do not want it to be overridden. I hope it will create a subclass;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
2.6. Data access: $this, self, parent keyword
$this: Pseudo variable, representing the class itself, accessible to this class and the parent class Member properties and methods.
self: access static member properties or methods in the class
parent: access member properties or methods of the parent class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
2.7. Interface: define common behavior methods of different classes, but do not Specific implementation, specific methods are implemented by subclasses;
For example: people can eat, animals can also eat, and even some plants can eat, but they eat in different ways, so you need to define an interface class at this time, The specific method is implemented by subclasses;
Define the interface keyword: interface
Implement the interface method keyword: implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
2.8. Polymorphism: For example, interface A has two implementations B and C , B and C can have different implementations of the methods defined in the A interface. This phenomenon is called polymorphism;
In the above example, the ICanEat interface defines an eat() method. Humans eat rice and monkeys eat bananas. . They all implement an "eat" method, but they have different behaviors when eating, which is called polymorphism;
2.9. Abstract class: intervenes between the interface and the definition of the class, allowing part of the class to The method is not implemented, but a part of the method that has the same function and will not change is implemented. However, no implemented methods are allowed in interface classes.
For example: both humans and animals have methods for eating and breathing. Except for eating, the breathing methods are the same. In this case, an abstract class needs to be defined to implement it.
Define abstract class keywords: abstract
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
3. Special practices of PHP object-oriented programming
Some magic methods specific to the PHP language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
Related Recommended:
PHP object-oriented notes——123 Illustrated static properties and static methods
Study on the three major characteristics of PHP object-oriented
Detailed example of implementation of PHP object-oriented guestbook function
The above is the detailed content of A brief discussion on object-oriented programming in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
