PHP Object-oriented strategy (i) _php Foundation of object-oriented Basic knowledge

Source: Internet
Author: User
Tags class definition
1. Object-oriented Concepts
Object-oriented programming (object oriented Programming,oop, object-oriented programming) is a computer programming architecture, the basic principle of OOP is that the computer program is composed of a single unit or object that can act as a subroutine, OOP Achieves the three goals of software engineering: reusability, flexibility and scalability. To achieve the overall operation, each object is able to receive information, process data, and send information to other objects. Object-oriented has always been a popular topic in the field of software development, first of all, object-oriented accords with the general law of human beings. Secondly, the application of object-oriented method can make each part of the system perform their duties and dens. Open a door for programmers to make their programming code simpler, easier to maintain, and more reusable. Some people say that PHP is not a real object-oriented language,
That's the truth. PHP is a hybrid language, you can use OOP, or you can use traditional procedural programming. However, for large projects, you might want to use pure OOP in PHP to declare classes, and use objects only in your project
and classes. This concept I do not say much, because there are many friends away from object-oriented programming is the main reason is a touch-oriented concept of the time to understand, so do not want to learn. Wait for the reader to read the full article and then go to the summary
Read it.
2. What is a class, what is an object, the relationship between a class and an object
Class is the concept of a class that is a collection of objects with the same properties and services. It provides a unified abstract description of all objects belonging to the class, including the two main parts of the property and service. In object-oriented programming languages, classes are
A separate program unit that should have a class name and include a description of the attribute and two main parts of the service description.
Object concept: An object is an entity that is used to describe objective things in the system, which is a basic unit of the system. An object consists of a set of properties and a set of services that operate on this set of properties. From a more abstract point of view, the
An abstraction of something in a problem domain or domain that reflects the information and role that the 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. The objective world is made up of objects
And the connection between the objects.
The relationship between a class and an object is like the relationship between a mold and a casting, and the instantiation of a class is an object, and the abstraction of a class of objects is a category. Class describes a set of objects that have the same attributes (properties) and the same behavior (method).
The above is probably the definition of it, maybe you are just contact with the object-oriented friends, do not be the concept of things dizzy, give you an example, if you go to Zhongguancun to buy a few assembled PC machine, where you first step to do,
is not installed by the engineer and you sit together, according to the information you provide with you to complete a installed configuration list Ah, this configuration can be imagined as a class, it is a piece of paper, but it is recorded on the PC you want to buy information, such as
Fruit with this configuration single buy 10 machines, then these 10 machines, are made up of this configuration list, so that the 10 machines is a type, it can be said to be a class. So what is an object, the instantiation result of the class is the object, with this
Configuration of a single configuration (instantiation) of the machine is the object, we can operate the entity, 10 machines, 10 objects. Each machine is independent, only to show that they are the same type of one of the machines do not do any action
affect the other 9 machines, but I modify the class, that is, in this configuration list with one or fewer accessories, then installed 9 of the machine has changed, this is the relationship between class and object (the class of the instantiation result is the object).
3. What is Object oriented programming?
Not to mention his concept, if you want to set up a computer classroom, the first to have a room, the room must have n computers, there are n tables, n chairs, whiteboard, projectors and so on, these are what, just we said, this is the object, can see the entity, Can say this computer classroom unit is this one entity object, they altogether
With the formation of this computer classroom, then we are to do the program, this and the object-oriented what relationship? Develop a system program and build a computer classroom like, you abstract each independent function module into classes, forming objects, composed of multiple objects
In this system, these objects interact with each other to receive information, process data, and send information to other objects.
4. How do I abstract a class?
It has been described above, object-oriented program units is the object, but the object is through the class instantiation, so the first thing we have to do is how to declare the class, make a class is easy, as long as master the basic program syntax
The rules of righteousness can be done, so the difficulty is there? How many classes to use for a project, how many objects to use, where to define the class, what class to define, how many objects are instantiated in the class, how many attributes are in the class,
How many methods and so on, this needs the reader through in the actual development in the actual problem analysis design and the summary.
The definition of the class:
Class Name {
}
Using a keyword class and the following plus a class name you want, plus a pair of curly braces, the structure of a class is defined, as long as you write code inside it, but what does it say? What can I write? How to Write is
A complete class? Above, the use of the class is to let it instance of the object to give us, it is necessary to know what kind of object you want, like the above we speak of a set of the list of what to write, you installed the machine is what
Mody. For example, a person is an object, how do you recommend a person you like to lead? Of course, the more detailed the better:
First, you will introduce the person's name, sex, age, height, weight, phone number, home address, and so on.
Then you have to introduce what this person can do, drive, speak English, use a computer, and so on. As long as you introduce a little more, others to this person a little more understanding, this is our description of a person, now I
To sum up, all the objects we use class to describe are similar, from the above human description can be seen, to make a class, from the point of view of the definition of two parts, the first is static description, the second is from the dynamic description, static description
That's what we're talking about, like the name, sex, age, height, weight, phone number, home address, and so on, as we've seen above.
Dynamic is the function of this object, such as the person can drive, speak English, can use computers and so on, abstract into the program, we put the dynamic written function or method, functions and methods are the same. So, all classes
Are written from both the attribute and the method, and the attribute is called the member property of the class, and the method is called the member method of the class.
Class Person {
Member properties: Name, sex, age, height, weight, telephone number, home address
Member method: Can drive, speak English, can use the computer
}
Property:
By declaring a variable by using the keyword "var" in the class definition, the property of the class is created, although the initial value can be given when declaring the member property, but it is not necessary to give the member property initial value when declaring the class, for example, if the
Person's name assigns "John", then uses this class example dozens of person, these dozens of people all call John, therefore does not have necessarily
To do this, we can give the member property the initial value after the object is instantiated.
such as: Var $somevar;
Method (member function):
By declaring a function in the class definition, you create a method of the class.
such as: function somefun (parameter list)
{ ... ... }
Code fragment
Copy Code code as follows:

<?php
Class person{
The following are the member properties of the person
var $name; Person's name
var $sex; The gender of the person
var $age; The age of the person
The following is a method of member of the person
function say () {
The way this man can talk.
echo "This man is talking";
function Run () {
The way this man can walk.
echo "This man is walking";
}
}
?>

The above is a class declaration, a class declared from properties and methods, but member properties are best declared
Do not give the initial value, because we do the person this class is a descriptive information that will be used to instantiate objects, than
If the example of 10 people object, then these 10 people, each person's name, sex, age are different,
So it's best not to assign an initial value to a member attribute in this place, but to assign values to each object separately.
The same way you can make the class you want, as long as you can use attributes and methods to describe the entity can
Defined as classes, to instantiate objects.
In order to strengthen your understanding of the class, we do a class, do a shape of the class, the shape of a wide range of points, we
Just make a rectangle, analyze it first, think about it from two aspects, what are the properties of the rectangle? The functions of a rectangle are
What the?
Copy Code code as follows:

Class Rectangle
{
The properties of the rectangle
The length of the rectangle;
The width of the rectangle;
The method of the rectangle
The circumference of the rectangle;
The area of the rectangle;
}

Code fragment
Copy Code code as follows:

<?php
Class rect{
var $kuan;
var $gao;
function Zhouchang () {
Compute the perimeter of the rectangle;
function Mianji () {
Compute the area of the rectangle;
}
}
?>

If you use this class to create more than one rectangular object, each Rectangle object has its own length and width, and can be derived from
The circumference and area of the self.
Declaration of class We'll be right here!!

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.