Class in PHP-what is a class before we elaborate on the concept of class, let's first talk about the concept of object-oriented Programming: Object-oriented Programming (Object-Oriented Programming, short for OOP) it is designed to create software to reuse code and better simulate the real-world environment, which makes it recognized as the winner of top-down programming. It adds an extension statement to the program to encapsulate the function into the "object" necessary for programming. The object-oriented programming language makes complex work organized and easy to write. It is a revolution, not for the objects themselves, but for their ability to process their work. Objects are not compatible with traditional programming and programming methods, but some object-oriented Objects make the situation worse. Unless the entire development environment is object-oriented, the benefits of the object have not brought much trouble. Some people may say that PHP is not a real object-oriented programming language. PHP is a hybrid language. You can use object-oriented programming or traditional procedural programming. However, for development of large projects, you may want to use pure Object-Oriented Programming in PHP to declare classes, and in your project development, only objects and classes are used. As the project grows, it may be helpful to use object-oriented programming. object-oriented programming code is easy to maintain, easy to understand, and reuse. These are the basis of software engineering. Applying these concepts in Web-based projects will become the key to future website success.
An Object is an abstraction of certain things in the problem domain or implementation domain. It reflects the information that needs to be stored in the system and its role; it is the encapsulation body of a set of attributes and a group of services that have the right to operate on these attributes. Objects must be understood in two ways: objects in the real world to be processed by the system; objects that computers do not directly process, but are processed by computers, this computer representation is also called an object. Simply put, a person is an object, and a ruler is also an object. When these objects can be directly represented by data, we call them attributes. The measurement unit of a ruler can be centimeter, meter, or foot. This measurement unit is the property of a ruler.
In PHP, we can define a Class, which refers to the set of variables and some functions that use these variables. PHP is a loose type language, so the type overload does not work, and the overload does not work by the number of parameters. Sometimes it is very good to overload constructors in the orientation, so that you can create objects using different methods (passing different numbers of parameters ). PHP is implemented through classes.
In PHP, information is encapsulated through classes. In PHP, the syntax for defining classes is:
The Code is as follows:
Class Class_name // In the object-oriented programming class, the first character of the class is used to uppercase and must comply with the naming rules of variables.
{
// Set of functions and variables
}
?>
When defining a class, you can define it according to your preferred format, but it is better to maintain a standard so that development will be more effective.
Data members are defined using the "var" declaration in the class. They have no type before assigning values to data members. A data member can be an integer, an Array, an associated Array, or an object.
The following is an example of a class definition:
The Code is as follows:
Class Student
{
Var $ str_Name; // name
Var $ str_Sex; // gender
Var $ int_Id; // student ID
Var $ int_English; // english score
Var $ int_maths; // mathematical score
}
?>
This is a simple example of a very common definition class, used to show students' learning scores. The class name is Student, and the Student class contains the basic attributes of a Student: name, gender, student ID, English score, and math score.
A function is called a function defined in a class. When using a member variable of a class in a function, you should use $ this-> var_name, where var_name refers to the variable declared in the class, otherwise, a function can only be a local variable. We first define an Input () function to assign an initial value to an object in the instance:
The Code is as follows:
Function Input ($ Name, $ Sex, $ Id, $ Englis, $ Maths)
{
$ This-> str_Name = $ Name;
$ This-> str_Sex = $ Sex;
$ This-> int_Id = $ Id;
$ This-> int_Englis = $ English;
$ This-> int_Maths = $ Maths;
}
Now we define a function named "ShowInfo ()" to print the basic situation of students:
The Code is as follows:
Function ShowInfo () // defines the ShowInfo () function
{
Echo ("Name: $ this-> str_Name
");
Echo ("Gender: $ this-> str_Sex
");
Echo ("Student ID: $ this-> int_Id
");
Echo ("English score: $ this-> int_English
");
Echo ("mathematical score: $ this-> int_Maths
");
}
The defined class must use the new keyword to generate the object:
$ A_student = new Student;
For example, to create an instance for an object named $ Wing and assign values, you can use the following code:
$ Wing = new Student; // use the new keyword to generate an object
$ Wing-> Input ("Wing", "male", 87 );
// Enter Wing's name, gender, student ID, English score, and math score respectively. The name and gender are character variables, so double quotation marks are required. Other numeric variables are not required.
Through the complete source code below, we can clearly see how classes are used in PHP:
The Code is as follows:
Class Student
{
Var $ str_Name;
Var $ str_Sex;
Var $ int_Id;
Var $ int_English;
Var $ int_maths;
Function Input ($ Name, $ Sex, $ Id, $ English, $ Maths)
{
$ This-> str_Name = $ Name;
$ This-> str_Sex = $ Sex;
$ This-> int_Id = $ Id;
$ This-> int_English = $ English;
$ This-> int_Maths = $ Maths;
}
Function ShowInfo ()
{
Echo ("Name: $ this-> str_Name
");
Echo ("Gender: $ this-> str_Sex
");
Echo ("Student ID: $ this-> int_Id
");
Echo ("English score: $ this-> int_English
");
Echo ("mathematical score: $ this-> int_Maths
");
}
}
$ Wing = new Student;
$ Wing-> Input ("Wing", "male", 87 );
$ Paladin = new Student;
$ Paladin-> Input ("paladin", "female", 59.5 );
$ Wing-> ShowInfo ();
$ Paladin-> ShowInfo ();
?>
The execution result should be as follows:
Name: Wing
Gender: male
Student ID: 33
English score: 95
Mathematical score: 87
Name: Paladin
Gender: Female
Student ID: 38
English score: 58
Mathematical score: 59.5
The existing PHP versions have greatly improved the support for Object-Oriented Programming compared with the previous ones, but the support is not complete yet, however, at this stage, PHP's support for object-oriented programming languages is not only conducive to the design of program structures, but also to the maintenance of programs.