"Getting Started with PHP object-oriented (OOP) programming" 4. How do I abstract a class?

Source: Internet
Author: User
Tags getting started with php

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:

12 class类名 {}

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.

1234 class人 {    成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址    成员方法:可以开车, 会说英语, 可以使用电脑}

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:

123 functionsomefun(参数列表) {    ... ...}
1234567891011121314151617 <?phpclassPerson {    // 下面是人的成员属性    var$name;  // 人的名子    var$sex;   // 人的性别    var$age;   // 人的年龄     // 下面是人的成员方法    functionsay() { // 这个人可以说话的方法        echo"这个人在说话";    }    functionrun() { // 这个人可以走路的方法        echo"这个人在走路";    }}?>

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?

123456789 class矩形 {    // 矩形的属性    矩形的长;    矩形的宽;    // 矩形的方法    矩形的周长;    矩形的面积;}

PHP Code:

1234567891011121314 <?phpclassRect {    var$kuan;    var$gao;    functionzhouChang() {        计算矩形的周长;    }    functionmianJi() {        计算矩形的面积;    }}?>

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.

"Getting Started with PHP object-oriented (OOP) programming" 4. How do I abstract a class?

Related Article

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.