Class in PHP-What is Class

Source: Internet
Author: User

Before explaining the concept of classes, let's talk about the concept of object-oriented Programming: Object-oriented Programming (Object-Oriented Programming, short for OOP) is designed to create software to reuse code, it has the ability to 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:
Copy codeThe Code is as follows: <? Php
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:
Copy codeThe Code is as follows: <? Php
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:
Copy codeThe 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 another function named "ShowInfo ()" to print the basic information of students:
Copy codeThe Code is as follows: function ShowInfo () // defines the ShowInfo () function
{
Echo ("Name: $ this-> str_Name <br>
");
Echo ("Gender: $ this-> str_Sex <br>
");
Echo ("Student ID: $ this-> int_Id <br>
");
Echo ("English score: $ this-> int_English <br>
");
Echo ("mathematical score: $ this-> int_Maths <br>
");
}
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:

Copy codeThe Code is as follows: <? 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 = $ English;
$ This-> int_Maths = $ Maths;
}
Function ShowInfo ()
{
Echo ("Name: $ this-> str_Name <br>
");
Echo ("Gender: $ this-> str_Sex <br>
");
Echo ("Student ID: $ this-> int_Id <br>
");
Echo ("English score: $ this-> int_English <br>
");
Echo ("mathematical score: $ this-> int_Maths <br>
");
}
}


$ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.