object-oriented
Object Oriented Programming (OOP, object-oriented programming) is a computer programming architecture. One of the basic principles of OOP is that a computer program is composed of a single unit or object that can function as a subroutine. OOP achieves the three goals of software engineering: reusability, flexibility, and scalability. In order to realize the overall operation, each object can receive information, process data and send information to other objects. Object-oriented has always been a hot topic in the field of software development. First of all, object-oriented is in line with the general rules of how humans look at things. Secondly, the use of object-oriented methods allows each part of the system to perform its duties and perform its duties. This opens the door for programmers to write code that is simpler, easier to maintain, and more reusable. Some people say that PHP is not a true object-oriented language, and this is true. PHP is a hybrid language, you can use OOP or traditional procedural programming. However, for large projects, you may need to use pure OOP to declare classes in PHP, and only use objects and classes in your project. I wo n’t say much about this concept, because many people are away from object -oriented programming that the main reason is that they can’t understand when they are in contact with the concept of objects, so they don’t want to learn it. Go figure it out.
Since we are learning object-oriented, let us first understand what is process-oriented.
Procedure Oriented
"Procedure Oriented" (Procedure Oriented) is a process-centered programming idea. "Process-oriented" can also be called "record-oriented" programming ideas. They do not support rich "object-oriented" features (such as inheritance, polymorphism), and they do not allow mixing of persistent state and domain logic.
It is to analyze the steps required to solve the problem, and then use functions to implement these steps step by step. When using them, just call them one by one.
Important advantages of process orientation
Readability
Reusability
Maintainability
Testability
Process-oriented means analyzing and finding solutions The steps required for the problem, and then use functions to implement these steps step by step. When using them, just call them one by one.
Object-oriented is to decompose the transaction that constitutes the problem into various objects. The purpose of establishing the object is not to complete a step, but to describe the behavior of something in the entire step of solving the problem.
For example, the following example
It can be clearly seen that object-oriented divides problems by functions, not steps. It is also drawing a chess game. Such behavior is dispersed in multiple steps in process-oriented design, and different drawing versions are likely to appear, because usually designers will make various simplifications taking into account the actual situation. In object-oriented design, drawing can only appear in the chessboard object, thus ensuring the unity of drawing.
The relationship between classes and objects
Object: An object is anything that people want to study. It can not only represent specific things, but also abstract rules, plans, or events. Objects have state, and an object uses data values to describe its state. Objects also have operations, which are used to change the state of the object. The object and its operations are the behavior of the object. Objects realize the combination of data and operations, so that data and operations are encapsulated in the unity of objects.
Class: The abstraction of objects with the same characteristics (data elements) and behavior (function) is a class. Therefore, the abstraction of an object is a class, and the concretization of a class is an object. It can also be said that an instance of a class is an object, and a class is actually a data type. Classes have attributes, which are abstractions of the state of objects, and use data structures to describe the attributes of the class. A class has an operation, which is an abstraction of the object's behavior, described by the operation name and the method to implement the operation.
The relationship between objects and classes:
The relationship between classes and objects is like the relationship between molds and castings. The result of the strength of the class is the object, and the abstraction of the object is the class, and the class description A group of objects with the same characteristics (properties) and the same behavior.
Member properties and member methods of the php class
Methods defined by the class
"; } function jiawu(){ return "家务
"; } }; ?>
Note: There must be a modifier in front of the member attribute of the class. If you don’t know how to use the declaration modifier, you can use var (keyword). If there are other modifiers, don’t use var.
Note: Member attributes cannot be expressions, variables, methods or function calls with operators. The declared member methods must be related to the object and cannot be some meaningless operations.
Instantiated object
The instantiation format of the class is as follows:
$Object name = new class name ([parameter]); The statement that the class becomes an object
The object name returned by class instantiation, used to refer to methods in the class.
· new: Keyword, indicating that a new object is to be created.
· Class name: Indicates the name of the class.
· Parameters: The constructor method of the specified class is used to initialize the value of the class. If there is no constructor defined in the class, PHP will automatically create a default constructor without parameters.
The source code provided innew.php can clearly see how to instantiate and access objects.
"; } function jiawu(){ return "家务
"; } } $boy1 = new boy(); $boy1->name = "张三"; $boy2 = new boy(); $boy2 ->name = "李四"; echo $boy1 -> sex."
"; echo $boy2 -> height."
"; ?>
-> symbol represents the class under the access object name.
Special object reference "this".
As long as it is a member of an object, you must use this object to access the internal and methods of this object.
name} 做饭
"; $this->jiawu(); } function jiawu(){ echo "家务
"; } } $boy1 = new boy(); $boy2 = new boy; $boy2 ->name = "李四"; $boy1->name = "张三"; $boy1->zuofan(); $boy2->zuofan(); ?>
Construction method
Constructor is a special method. It is mainly used to initialize the object when creating the object, that is, to assign initial values to the object member variables. It is always used together with the new operator in the statement to create the object.
1. It is the first method that is automatically called after the object is created (special)
2. The method name is special and can be the same as the class name
3. Used to assign initial values to members in objects.
name="$name"; $this->age="$age"; $this->sex="$sex"; } //函数(成员方法) public function zuofan(){ echo "{$this->name} 做饭
"; $this->jiawu(); } function jiawu(){ echo "家务
"; } } $boy1 = new boy("名字",28); $boy2 = new boy("名",26,"男"); $boy1->zuofan(); $boy2->zuofan(); ?>
Destruction method
Destructor (destructor) and constructor On the contrary, when the object ends its life cycle (for example, the function in which the object is located has been called), the system automatically executes the destructor.
PHP 5 introduced the concept of destructor, which is similar to other object-oriented languages. Its syntax format is as follows:
void __destruct (void)
Inheritance
PHP uses the keyword extends to inherit a class. PHP does not support multiple inheritance. The format is as follows:
class Child extends Parent {
// Code part
}
?>
Access control
PHP pair attributes or Access control of methods is achieved by adding the keywords public, protected or private in front.
· PUBLIC (Public): Public members can be accessed anywhere.
# · Protected: The protected members can be accessed by them and their subclasses and parent class.
· Prive (private): Private class members can only be accessed by their class.
You can view public.php protected.php private.php to see the difference
Method rewriting
Method overloading ( override)/coverage - when to use it: When the parent class knows that all subclasses need to use a method, but the parent class does not know how to write this method, you need to use method overloading. At this time, you can let the subclass override this method.
Popular example - the parent class (animal) knows that its subclasses (cats and dogs) can bark, but their barks are different, so the parent class cannot write this method and can only let the children classes (cat and dog) to define. The code is as follows:
Interface
Interface interface is a stipulation, something that people can inherit, a bit like an abstract class
The methods defined in it are not instantiated, but other classes need to implement it, and the interface must be implemented one by one. All methods defined, for example
interface Shop
{
public function buy($gid);
public function sell($gid);
public function view($gid);
}
I declare a shop interface class and define three methods: buy, sell, and view. Then all subclasses that inherit this class must implement these three methods. None of them will work. If the subclass does not implement these, it will not work. In fact, the interface class is, to put it bluntly, the template of a class and the regulations of a class. If you belong to this category, you must follow my regulations. No matter how you do it, I don’t care how you do it. That’s up to you. Things like:
Constant
You can define a value that always remains unchanged in the class as a constant. There is no need to use the $ symbol when defining and using constants.
The value of a constant must be a fixed value and cannot be a variable, class attribute, the result of a mathematical operation or a function call.
showConstant(); echo $class::constant . PHP_EOL; // 自 PHP 5.3.0 起 ?>
The meaning of:: in php
Reference methods of static methods and static properties in classes
For example,
You can use Test:: directly without instantiating the object. $test to get the value of the $test attribute
The same goes for static method calls Test::test(); Directly call the static method test
Abstract class
For PHP programmers, the most difficult point to master is the knowledge point of PHP abstract class application. As a novice, I am not yet ready to use object-oriented knowledge to program, but in future development, it is inevitable to use classes to encapsulate or use interfaces to develop programs in various modular formats.
In natural language, we understand the abstract concept as a large description of an object, which is a common characteristic for a certain type of object. The same is true in PHP. When we abstract a class, we can indicate the general behavior of the class. This class should be a template, which indicates some behaviors that its sub-methods must implement.
Definition of PHP abstract class application:
abstract class ClassName{
}
Key points of PHP abstract class application:
1. Define some methods, and subclasses must fully implement all methods in this abstraction
2. Objects cannot be created from abstract classes, their meaning is to be extended
3. Abstract classes usually have abstract methods , there are no braces in the method
Key points of PHP abstract class application:
1. Abstract methods do not have to implement specific functions, and are completed by subclasses
2. In subclasses When implementing a method of an abstract class, the visibility of its subclass must be greater than or equal to the definition of the abstract method
3. The method of the abstract class can have parameters or be empty
4. If If the abstract method has parameters, then the implementation of the subclass must also have the same number of parameters
PHP abstract class application example:
abstract public function_name(); //Note that there are no curly brackets
PHP abstract class rules:
1. As long as a class contains at least one abstract method, it must be declared as an abstract class
2. Abstract methods, no Can contain a function body
3. Inheriting a subclass of an abstract class and implementing an abstract method must have the same or lower access level as the abstract method
4. Inheriting a subclass of an abstract class Class, if all abstract methods are not implemented, then the subclass is also an abstract class
Let’s implement a simple abstract class: calculate the area of a rectangle. This rectangle can be extended from the shape class.
You can view the abstract.php code
Final keyword
If we don’t want a class to be inherited, we use final to modify the class. This class will not be inherited.
final---used before classes and methods.
Final class---cannot be inherited.
Final method---cannot be overridden.
Final classes cannot be inherited.
When we only want a class to be non-extensible, we can add Final in front of the class, and the class cannot be inherited.
Static keyword
Another important feature of variable scope in php is static variables (static variables). Static variables only exist in the local function scope and are only initialized once. When the program execution leaves this scope, its value will not disappear, and the result of the last execution will be used.
PHP_EOL
Line break
Call the parent class constructor method
parent::__construct().
PHP will not automatically call the constructor of the parent class in the constructor of the subclass. To execute the constructor of the parent class, you need to call