A deep understanding of the concept of PHP Class. For example, a vehicle can define properties such as color, number of tires, manufacturer, model, and capacity, and define behavior such as stop, forward, turn, and whistle. In OOP terminology, entities such as a vehicle can define properties such as color, number of tires, manufacturer, model, and capacity, and define actions such as stop, forward, turn, and whistle. In OOP terminology, a specific definition of the nature and behavior of an object is called a class ).
Class definition and creation
A class is a set of objects with the same attributes and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: Attributes and methods. In an object-oriented programming language, a class is an independent program unit. it should have a class name and contain two main parts: attribute description and method description.
Class is used to indicate the actual things to be processed in the application. For example, if you want to create an application to manage a public library, you may need to include classes to indicate books, magazines, employees, special events, customers, and other things that need to be managed. Each object contains a set of properties and actions, which are called fields and methods in OOP. they define entities. The general class creation syntax in PHP is as follows:
The code is as follows:
Class Class_Name
{
// Field declaration
// Method declaration
}
Create a class:
The code is as follows:
Class Employee
{
Private $ name;
Private $ title;
Protected $ wage;
Protected function clockIn (){
Echo "Member $ this-> name clocked in at". date ("h: I: s ");
}
Protected function clockOut (){
Echo "Member $ this-> name clocked out at". date ("h: I: s ");
}
}
This class is named Employee and defines three fields: name, title, and wage. It also defines two methods: clockIn and clockOut ).
Class applications
A class that defines attributes and methods is a complete class. it can contain a complete processing logic in a class. Use the new keyword to instantiate an object to apply the logic in the class. Multiple objects can be instantiated at the same time.
Class instantiation:
The code is as follows:
Object = new class_name ();
After an object is instantiated, use the-> operator to access the member attributes and methods of the object. For example:
The code is as follows:
Object-> var_name;
Object-> function_name;
To access the attributes or methods of a member in a defined class, you can use the pseudo variable $ this. $ This indicates the current object or the object itself.
The code is as follows:
Class Person {
// Member attributes of a person
Var $ name; // The name of a person.
Var $ age; // age of a person
// The "say ()" method of a member of a person
Function say (){
Echo "My name is:". $ this-> name ."
";
Echo "My age is:". $ this-> age;
}
}
// Class definition ends
$ P1 = new Person (); // instantiate an object
$ P1-> name = "Gonn"; // assign a value to the $ p1 object attribute
$ P1-> age = 25;
$ P1-> say (); // call the say () method in the object
?>
Program running result:
The code is as follows:
My name is Gonn.
My age is: 25
Bytes. In OOP terminology, entities...