PHP Object-oriented all-in-one (i) Object-oriented basic knowledge _php tutorial

Source: Internet
Author: User
1. Object-oriented Concepts
Object-oriented programming (object oriented Programming,oop, OOP) is a computer programming architecture, and one of the basic principles of OOP is that a computer program is composed of a single unit or object that can act as a subroutine, OOP It achieves the three goals of software engineering: reusability, flexibility, and extensibility. To achieve the overall operation, each object can receive information, process data, and send information to other objects. Object-oriented has always been a hot topic in the field of software development, first of all, object-oriented conforms to the general law of human beings ' view of things. Secondly, the use of object-oriented method can make all parts of the system perform their duties and his ability. Opens the 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 mixed language, you can use OOP, or you can use traditional procedural programming. However, for large projects, you may need to use pure OOP in PHP to declare classes, and only use objects in your project
and classes. I'm not going to say much about this concept, because there are a lot of friends away from object-oriented programming is the main reason is a contact with the object-oriented concept when the understanding is not up, so do not want to learn. When the reader reads the whole article,
Read it, understand.
2. What is a class, what is the relationship between the object, the class, and the object
Class Concept: A class is a collection of objects that have the same properties and services. It provides a unified, abstract description of all objects that belong to the class, including two main parts of properties and services. In object-oriented programming languages, classes are
A separate program unit, which should have a class name and include both the attribute description and the service description in the main sections.
Object concept: An object is an entity used to describe an objective thing in a system, which is a basic unit of a system. An object consists of a set of properties and a set of services that operate on that set of properties. From a more abstract point of view, the
Like a problem domain or an abstraction of something in the implementation domain, it reflects the information that the thing needs to be saved in the system and the role it plays; it is a set of attributes and a package of services that have permission to operate on those properties. The objective world is made up of objects
And the connection between 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 the object, and the abstraction of a class object is the classes. Class describes a set of objects that have the same attributes (attributes) and the same behavior (methods).
The above is probably their definition, maybe you are just contact object-oriented friends, not to be confused by the concept of things, give you an example, if you go to Zhongguancun to buy a few assembled PC, to where you first step to do,
is not the installation of the engineer and you sit together, according to the information you provide with you to complete an installed configuration of a single 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 according to this configuration single composition, so that the 10 machine is a type, can be said to be a class. So what is an object, the instantiation 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, can only show that they are the same class, to one of the machines do any action will not
affect the other 9 machines, but I modify the class, that is, in this configuration list with one or fewer accessories, then the installed 9 machine has changed, this is the relationship between the class and the object (the instantiation of the class is the object).
3. What is object-oriented programming?
Do not say his concept, if you want to build a computer classroom, first to have a room, the room to have n computers, there are n tables, n chairs, white boards, projectors and so on, these are what, just now we said, this is the object, can see the entity, It can be said that the unit of the computer classroom is one of the entity objects, they all
Together with this computer classroom, then we are doing the procedure, what does this have to do with object-oriented? Develop a system program and build a computer classroom. Similarly, you abstract each individual function module into a class, forming an object, composed of multiple objects
This system, which is capable of receiving information, processing data, and sending information to other objects, can interact with each other.
4. How do I abstract a class?
As described above, the object-oriented program is the object, but the object is also through the instantiation of the class, so the first thing we have to do is how to declare the class, to make a class is very easy, as long as master the basic program syntax set
The rules of righteousness can be done, then 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 the class instantiates, how many properties there are in the class, and
How many methods and so on, this requires the reader through the actual development of the actual problem analysis design and summary.
Definition of the class:
Class Name {
}
Use a keyword class and followed by a class name you want and a pair of curly braces, so that 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 out of the object for us to use, it is necessary to know what you want to be the object, like the above we talk about a single installed on the list of what to write, you installed the machine will have what
You. For example, a person is an object, how do you recommend a person you are optimistic to your leadership? Of course the more detailed the better:
First, you'll introduce the person's name, gender, age, height, weight, phone, home address, and so on.
Then, you want to introduce what this person can do, can drive, speak English, can use computers 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
We summarize that all the objects we use class to describe are similar, from the above person's description can be seen, to make a class, from the point of view of the definition of two parts, the first is from the static description, the second is from the dynamic description, static description
That's what we're talking about, like the person's name, gender, age, height, weight, phone, home address, and so on, as we've seen above.
Dynamic is the function of this object, such as this person can drive, can speak English, can use computer and so on, abstract into a program, we put the dynamic writing function or method, function and method is the same. Therefore, all classes
are written in terms of properties and methods, and properties are called member properties of the class, and the method is called the member method of the class.
Class Person {
Member properties: Name, gender, age, height, weight, phone, home address
Member method: Can drive, speak English, can use the computer
}
Property:
Declaring a variable by using the keyword "var" in the class definition creates a property of the class, although it can be given an initial value when declaring a member property, but it is not necessary to give the member property the initial value when declaring the class, such as
The name of the person "Zhang San", then use this class instance out of dozens of people, these dozens of people are called Zhang San, so no must
Yes, we can give the member property the initial value after the instance is out of the object.
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 Snippets
Copy CodeThe code is as follows:
Class person{
The following is the person's member property
var $name; Man's name
var $sex; Man's Sex
var $age; The age of the person
The following is a member method 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 a property and a method, but the member property is best declared
Do not give the initial value, because we do the person this class is a descriptive information that will be instantiated with it in the future, than
If the instantiation of 10 people, then the 10 people, each person's name, gender, age is not the same,
Therefore, it is best not to assign the initial value to the member attribute in this place, but to assign each object the values individually.
The same way you can make the class you want, as long as you can use attributes and methods to describe the entity can be
Defined as a class to instantiate an object.
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, first analysis, think about the two aspects of analysis, what are the properties of the rectangle? The functions of the rectangle are
What the?
Copy CodeThe code is as follows:
Class Rectangle
{
Properties of the Rectangle
The length of the rectangle;
width of rectangle;
Method of Rectangle
The perimeter of the rectangle;
The area of the rectangle;
}

Code Snippets
Copy CodeThe code is as follows:
Class rect{
var $kuan;
var $gao;
function Zhouchang () {
Calculates the perimeter of a rectangle;
} function Mianji () {
Calculates the area of a rectangle;
}
}
?>

If you use this class to create more than one rectangle object, each Rectangle object has its own length and width, which can be derived from
The circumference and area of their own.
Declaration of class let's get here!!

http://www.bkjia.com/PHPjc/320659.html www.bkjia.com true http://www.bkjia.com/PHPjc/320659.html techarticle 1. Object-oriented programming (object oriented Programming,oop, object-oriented programming) is a computer programming architecture, and one of the basic principles of OOP is computer 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.