Before we explain the concept of class, let's talk about the concept of object-oriented programming: Object-oriented program design (object-oriented programming, précis-writers as OOP) is intended to create software reuse code, with the ability to better simulate the real-world environment, This makes it recognized as the winner of top-down programming. It "encapsulates" a function into an "object" necessary for programming by adding an extension statement to the program. The object-oriented programming language makes complex work clear and easy to write. It is a revolution, not to the object itself, but to the ability of them to deal with their work. Objects are not compatible with traditional programming and programming methods, but are only partially object-oriented and make the situation worse. Unless the entire development environment is object-oriented, the benefits of the object do not have much trouble. One might say that PHP is not a true object-oriented programming language, PHP is a hybrid language, you can use object-oriented programming, or you can use traditional procedural programming. However, for the development of large projects, you might want to use pure object-oriented programming in PHP to declare classes, and only use objects and classes in your project development. As the project grows larger, using object-oriented programming can be helpful, object-oriented programming code is easy to maintain, easy to understand and reuse, which is the foundation of software engineering. Applying these concepts to Web-based projects is the key to future site success.
Object is an abstraction of a problem domain or something in the implementation domain that reflects the information and the role that this thing needs to be saved in the system; it is a set of attributes and a package of services that have permission to manipulate these properties. The object is to be understood in two ways: on the one hand refers to the real-world objects that the system is dealing with; On the other hand, objects are objects that are not directly processed by the computer, but are handled by the corresponding computer representations, which are also referred to as objects. Simply put, a person is an object, a ruler can also be said to be an object. When these objects can be directly represented by the data, we call him property, the ruler can be measured in centimeters, meters or feet, and this unit of measure is the ruler's attribute.
In PHP we can define a class that refers to a variable and a collection of functions that use those variables. PHP is a loosely typed language, so overloading by type overloading does not work, and it does not work with different numbers of parameters. Sometimes it is good to overload constructors in orientation, so that you can create objects in different ways (passing different numbers of parameters). In PHP, this is done through classes.
In PHP, a class is done to encapsulate the information, and the syntax for defining a class in PHP is:
Copy CodeThe code is as follows: Class Class_name//In an object-oriented programming class, the first character in the customary class is uppercase and must conform to the naming convention of the variable.
{
A collection of functions and variables
}
?>
When defining a class, you can define it in the format you prefer, but it's better to keep a standard so that it's more efficient to develop.
Data members are defined in a class using the "var" declaration, which is not typed until the data member is assigned a value. A data member can be an integer, an array, an associated array (associative array), or an object.
The following is a practical example of a class definition:
Copy CodeThe code is as follows: Class Student
{
var $str _name; Name
var $str _sex; Gender
var $int _id; School Number
var $int _english; English score
var $int _maths; Math Scores
}
?>
This is a simple example of a common definition class that shows the student's academic performance, and the class name Student,student class includes a student's basic attributes: Name, gender, school number, English score, and math score.
Functions, which we call a function defined in a class, when accessing a class member variable in a function, you should use $this->var_name, where var_name refers to a variable declared in a class, otherwise it can only be a local variable for a function. We first define a function of input () 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 let's define a function called "showinfo ()" To print the student's basic situation:
Copy CodeThe code is as follows: function showinfo ()//define SHOWINFO () functions
{
Echo ("Name: $this->str_name
”);
Echo ("Gender: $this->str_sex
”);
Echo ("Study No.: $this->int_id
”);
Echo ("English score: $this->int_english
”);
Echo ("Math score: $this->int_maths
”);
}
A well-defined class must use the new keyword to generate the object:
$A _student=new student;
For example, to create an instance of an object named $wing and assign a value, you can use the following code:
$Wing =new Student; Use new keywords to generate objects
$Wing->input ("Wing", "male", 33,95,87);
Enter Wing's name, gender, school number, English score, and math score respectively, where the name and gender are character variables, so double quotes are required, and the others are numeric variables.
With this complete source code, we can clearly see how the class is being used in PHP:
Copy CodeThe 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 ("Study No.: $this->int_id
”);
Echo ("English score: $this->int_english
”);
Echo ("Math score: $this->int_maths
”);
}
}
$Wing = new Student;
$Wing->input ("Wing", "male", 33,95,87);
$Paladin = new Student;
$Paladin->input ("Paladin", "female", 38,58,59.5);
$Wing->showinfo ();
$Paladin->showinfo ();
The result of?> execution should be this:
Name: Wing
Gender: Male
Study No.: 33
English Score: 95
Math Score: 87
Name: Paladin
Gender: Female
Study No.: 38
English Score: 58
Math Score: 59.5
PHP's existing version of the previous version of the support for object-oriented programming has been greatly improved, but the support is not very complete, but at this stage, PHP support for the object-oriented programming language is not only conducive to our design program structure, for the maintenance of the program can also provide a great help.
http://www.bkjia.com/PHPjc/317099.html www.bkjia.com true http://www.bkjia.com/PHPjc/317099.html techarticle before we elaborate on the concept of class, let's talk about the concept of object-oriented programming: Object-oriented program design (object-orientedprogramming, précis-writers for OOP) to create software reuse code, with ...