Php--opp--4. How to abstract a class?

Source: Internet
Author: User

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 the basic rules of the program syntax 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, how many methods are there, and so on, which requires the reader to design and summarize the actual problem analysis in the actual development. Definition of the class:

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 put out what the machine. 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, and now we summarize, all objects we use class to describe are similar, from the description of the above can be seen, to make a class, from the definition of the angle of two parts, the first is static description, The second is the dynamic description, the static description is what we call the property, as we see above, the person's name, gender, age, height, weight, telephone, home address and so on. 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, use the computer

Property:

Declaring a variable by using the keyword "var" in the class definition, that is, creating a property of the class, although it is possible to give the initial value when declaring a member property, it is not necessary to declare the class with the initial values of the member property, for example, if the name of the person is "Zhang San", then use this class instance for dozens of people These dozens of people are called Zhang San, so it is not necessary that we give the member property 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:

1 functionsomefun(参数列表)
2 {
3     ... ...
4 }
01 <?php
02 classPerson
03 {
04     // 下面是人的成员属性
05     var$name;  //人的名子
06     var$sex;   //人的性别
07     var$age;   //人的年龄
08
09     // 下面是人的成员方法
10     functionsay() // 这个人可以说话的方法
11     {
12         echo"这个人在说话";
13     }
14
15     functionrun() // 这个人可以走路的方法
16     {
17         echo"这个人在走路";
18     }
19 }
20 ?>

The above is a class declaration, a class declared from the properties and methods, but the member property is best not to give the initial value at the time of declaration, because we do the person this class is a descriptive information, in the future use it to instantiate objects, such as the instantiation of 10 people object, then these 10 people, each person's name, gender, Age is 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 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 the object.

In order to strengthen your understanding of the class, we do a class, to do a shape of the class, the shape of a wide range of points, we do a rectangular bar, first analysis, think of two aspects of analysis, the properties of the rectangle what? What are the functions of a rectangle?

01 class矩形
02 {
03     // 矩形的属性
04     矩形的长;
05     矩形的宽;
06
07     // 矩形的方法
08     矩形的周长;
09     矩形的面积;
10 }

PHP Code:

01 <?php
02 classRect
03 {
04     var$kuan;
05     var$gao;
06
07     functionzhouChang()
08     {
09         计算矩形的周长;
10     }
11
12     functionmianJi()
13     {
14         计算矩形的面积;
15     }
16 }
17 ?>

If you use this class to create multiple rectangular objects, each Rectangle object has its own length and width, and you can find your perimeter and area.

Reprint: Original address: php-note.com

Php--opp--4. How to abstract a class?

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.