Home>Article>Backend Development> PHP object-oriented simple summary

PHP object-oriented simple summary

WJ
WJ forward
2020-05-29 10:55:06 2353browse

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

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

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 usingprivatekeyword to encapsulate these properties and methods.
As follows:

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

$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()

$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

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

Abstract technology

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

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

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.net. If there is any infringement, please contact admin@php.cn delete