Classes in PHP-what is called Class (reprint)------shortcuts to Getting Started

Source: Internet
Author: User
Tags define array definition class definition functions variables variable domain
Classes in PHP-what is called class

Linuxaid 01-03-08 10:16 1594p Wing
--------------------------------------------------------------------------------


Before we explain the concept of class, let's talk about the concept of object-oriented programming: Object-oriented Program Design (object-oriented programming, denoted for OOP) is designed 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 the function into the "objects" necessary for programming by adding an extension statement to the program. Object-oriented programming languages make complex work clear and easy to write. Say it is a revolution, not for the object itself, but for the ability to deal with it. Objects are not compatible with traditional programming and programming methods, but only partially object-oriented can make the situation worse. Unless the entire development environment is object-oriented, the benefits of objects have not been much of a hassle. One might say that PHP is not a real object-oriented programming language, PHP is a hybrid language, you can use object-oriented programming, you can also use traditional procedural programming. However, for the development of large projects, you may want to use pure object-oriented programming in PHP to declare classes, and to develop only objects and classes in your project. As projects become larger, object-oriented programming can be helpful, and 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 success in future Web sites.
An object is an abstraction of something in the problem domain or implementation domain that reflects the information and the role that this thing needs to be saved in the system; it is a package of properties and a set of services that are authorized to operate on these properties. There are two ways to understand objects: on the one hand, the object in the real world that the system is dealing with, on the other hand, the object is the object that the computer is not directly dealing with, but rather the computer representation, which is also called an object. 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 data, we call him attributes, and the ruler can be measured in centimeters, meters or feet, which is the property of the ruler.
In PHP we can define a class, class, which refers to a set of variables and functions that use those variables. PHP is a loosely typed language, so it doesn't work through type overloading, and overloading does not work with the number of arguments. It is sometimes very good to overload constructors in the face, so that you can create objects in different ways (passing a different number of arguments). This is done in PHP by a class.
In PHP, a class is used to complete information encapsulation, and the syntax for defining classes in PHP is:
<?php
Class Class_name//In object-oriented programming classes, the first character of the custom class is capitalized and must conform to the naming rules for the variable.
{

A collection of functions and variables

}
?>
When you define a class you can define your own preferences, but it's best 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:
<?php
Class Student
{
var $str _name; Name
var $str _sex; Gender
var $int _id; School Number
var $int _english; English results
var $int _maths; Math Scores
}
?>
This is a simple example of a very common definition class, used to show students ' academic achievement, class name Student,student class contains a student's basic attributes: Name, gender, number, English score and math grade.
function we call the functions defined in a class, when you access a class member variable in a function, you should use $this->var_name, where var_name refers to the variable declared in the 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:
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're going to define a function called "showinfo ()" To print the student's basic information:
function Showinfo ()//define SHOWINFO () functions
{
Echo ("Name: $this->str_name<br>
”);
Echo ("Sex: $this->str_sex <br>
”);
Echo ("School Number: $this->int_id <br>
”);
Echo ("English score: $this->int_english <br>
”);
Echo ("Math score: $this->int_maths <br>
”);
}

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 it, you can use the following code:
$Wing =new Student; Use new keyword to generate object
$Wing->input ("Wing", "male", 33,95,87);
Enter Wing's name, gender, study number, English score, Math score, and the name and gender are character variables, so double quotes are required, and other values are not required.
With this complete source code, we can see exactly how the class is being used in PHP:

<?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 ("Sex: $this->str_sex <br>
”);
Echo ("School Number: $this->int_id <br>
”);
Echo ("English score: $this->int_english <br>
”);
Echo ("Math score: $this->int_maths <br>
”);
}
}


$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 the execution should be this:
Name: Wing
Sex: Male
School Number: 33
English Score: 95
Math Score: 87
Name: Paladin
Sex: Female
School Number: 38
English Score: 58
Math Score: 59.5

The existing version of PHP has improved significantly in support of object-oriented programming than previous versions, but the support is not very complete, but at this stage of PHP's support for object-oriented programming language is not only conducive to our design program structure, for the maintenance of the program can also provide a lot of help.



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.