Class in PHP - What Class

xiaoxiao2021-03-06  61

Before I explain the concept of object-oriented programming: object-oriented programming (Object-Oriented Programming, discourse to OOP) is intended to create software reuse code, with better analog real-world environment This makes it recognized as a winner programmed from the top. It adds the "Object" necessary to program the function "package" to the program. Object-oriented programming languages ​​make complex work conditions clear, easy to write. Say it is a revolution, not for the object itself, but the ability to deal with work. Objects are not compatible with traditional programming and programming methods, just partially oriented objects will make the situation worse. Unless the entire development environment is object-oriented, the benefits of objects have not brought more troublesome. Some people may say PHP is not a real object-oriented programming language. PHP is a hybrid language, you can use object-oriented programming, or use traditional procedural programming. However, for the development of large projects, you may want to use pure object-oriented programming to declare classes in PHP, and use only objects and classes in your project. As projects are getting bigger, using object-oriented programming may be helpful, object-oriented programming code is easy to maintain, easy to understand and reuse, these are the foundation of software engineering. Applying these concepts in web-based projects will become the key to the success of future websites.

Object (Object) is an abstraction of a problem domain or implementing certain things in the domain, which reflects the information that needs to be saved in the system and the role; it is a set of attributes and a set of people who have the right to operate these attributes Service package. About objects should be understood from two aspects: one aspect refers to an object in the real world to be processed; on the other hand, the object is an object that the computer is not directly processed, but to process the corresponding computer representation, this computer representation is also referred to as an object. Simply, a person is an object, a ruler can also be said to be an object. When these objects can be represented by data, we call him attributes, and the measurement unit of the ruler can be centimeter, meter or feet, this metric is the attribute of the ruler.

In PHP we can define a class, class (Class) means a collection of variables and some functions that use these variables. PHP is a loose type language, so it does not work by the number of heavy doldes, and the number of parameters is not active. Sometimes the overloaded constructor is very good, so you can create objects through different methods (passing different quantities). In PHP is achieved by class.

In the PHP, the syntax of the class is defined in the PHP in PHP.

Class class_name // In an object-oriented programming class, the first character of the custom class is uppercase and must meet the naming rules of the variable.

{

// Competition of functions and variables

}

?>

When you define a class, you can define your own format, but it is best to maintain a standard so that it will be more effective in developing.

Data members are defined using the "VAR" declaration in the class. They are not type before assigning the data member. A data member can be an integer, an array, a associated array (Associative Array) or an object.

Below is an actual example of a class definition:

Class Student

{

VAR $ STR_NAME; // Name

Var $ STR_SEX; // Gender

Var $ int_id; // student number

Var $ int_ENGLISH; // English score

Var $ int_maths; // Mathematics results

}

?>

This is a simple example of a common definition class, which is used to display students' learning results. Classified Student, the Student class contains a basic attribute of a student: name, gender, school number, English score and mathematics. Function We call the function defined in the class, when you access the class member variable in the function, you should use $ this-> var_name, where var_name refers to the declared variables in the class, otherwise a function, It can only be partial variables. Let's define an input () function, used to assign the initial value to the objects in the instance:

Function Input ($ Name, $ SEX, $ ID, $ ENGLIS, $ MATHS

{

$ this-> STR_NAME = $ Name;

$ this-> str_sex = $ sex;

$ this-> int_id = $ ID;

$ this-> int_englis = $ 中文;

$ this-> INT_MATHS = $ MATHS;

}

Now let's define a function called "showinfo ()" for printing the basic situation of the student:

Function showinfo () // Define showinfo () functions

{

echo ("Name: $ this-> str_name

");

echo ("Gender: $ this-> STR_SEX

");

echo ("Learn: $ this-> int_id

");

echo ("English score: $ this-> int_english

");

echo ("Mathematics Score: $ this-> INT_MATHS

");

}

The defined class must use the New keyword to generate an object:

$ A_student = new student;

For example, we have to create an instance for an object called $ Wing, and assign a value, you can use the following code:

$ Wing = new student; // uses new keywords to generate objects

$ Wing -> Input ("Wing", "Men", 33, 95, 87);

// Enter Wing's name, gender, school number, English score, mathematical achievement, where the name and gender are character variables, so it is necessary to use double quotes, and other value-type variables are not needed.

Through the complete source code below, we can clearly see how the class is used in PHP:

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 = $ 中文;

$ this-> INT_MATHS = $ MATHS;

}

Function showinfo ()

{

echo ("Name: $ this-> str_name

");

echo ("Gender: $ this-> STR_SEX

");

echo ("Learn: $ this-> int_id

");

echo ("English score: $ this-> int_english

");

echo ("Mathematics Score: $ this-> INT_MATHS

");

}

}

$ Wing = new student; $ wing-> INPUT ("Wing", "Men", 33, 95, 87);

$ Paladin = new student;

$ Paladin-> INPUT ("Paladin", "Female", 38, 58, 59.5);

$ WING-> Showinfo ();

$ Paladin-> showinfo ();

?>

The execution result should be like this:

Name: wing

Sex: Male

Learning number: 33

English score: 95

Mathematics score: 87

Name: paladin

Gender: Female

Learn: 38

English score: 58

Mathematics score: 59.5

The existing version of PHP has great improvement in the previous version, but support is not very complete, but the support of PHP to object programming languages ​​is not only beneficial to our design procedures. The structure provides great help to the maintenance of the program.

Full article:

CNGNU

转载请注明原文地址:https://www.9cbs.com/read-112387.html

New Post(0)