Php object-oriented tutorial 2

Source: Internet
Author: User
Php object-oriented tutorial 2 4. how to abstract a class?
As mentioned above, the unit of the object-oriented program is the object, but the object is instantiated through the class, so
The first thing we need to do is how to declare a class. it is easy to create a class. you only need to master the basic program syntax and define rules.
Where are the difficulties? How many classes are used and how many objects are used in a project? define classes and definitions
What kind of class is required? how many objects are produced by this class instance, how many attributes and methods are available in the class?
Readers should analyze and design the actual problems in actual development.
Class definition:

Class name {}

Add a class name you want and a pair of braces to the end of a keyword class.
It is defined. you only need to write code in it, but what is written in it? What can I write? How to write is complete
Class? As mentioned above, the use of classes is to let it instance out of objects for us to use, so we need to know what kind of pair you want
As we have mentioned above, what is written on an installation configuration sheet is what the machines you have installed have. For example, a person is
How do you recommend someone you like to your leadership? Of course, the more detailed the better:
First, you will introduce the person's name, gender, age, height, weight, phone number, home address, and so on.
Then, you will introduce what this person can do. he can drive, speak English, and use a computer.
As long as you have a little introduction, others will have a better understanding of this person. this is our description of a person.
We can describe all objects using classes in a similar way. from the descriptions above, we can see that a class is made from
The meaning is divided into two parts: static description, dynamic description, and static description.
Attributes, as we can see above, the person's name, gender, age, height, weight, phone number, home address, and so on. Dynamic
This is the function of a person's object. for example, a person can drive, speak English, use a computer, etc,
We dynamically write functions or methods. functions and methods are the same. Therefore, all classes are from the attribute and method
Attribute is also called the member attribute of this class. the method is called the member method of this class.

Class member {Member attributes: name, gender, age, height, weight, phone number, home address member method: can drive, speak English, can use computer}

Attribute:
The variable is declared by using the keyword "var" in the class definition, that is, the class attributes are created, although the member attributes are declared
The initial value can be given, but it is unnecessary to give the member attribute the initial value when declaring the class. for example,
The name of a person is assigned to "Michael Jacob", so dozens of people are named Michael Jacob using this class instance.
It is not necessary. after the instance outputs an object, we can give the initial value of the member attribute.
For example:

Var $ somevar; Method (member function): A class method is created by declaring a function in the class definition. For example, function somefun (parameter list ){......}
 

The above is the declaration of a class, a class declared from the attributes and methods, but it is best to declare the member attributes
Do not give the initial value, because the class we are working on is a descriptive information and will be used to instantiate objects, such as instantiation.
For 10 people, the names, gender, and age of these 10 people are different, so it is best not
Assign an initial value to the member attribute, but assign values to each object separately.
You can use the same method to make the class you want. as long as you can use the attributes and methods to describe the objects, you can define them
Class to instantiate the object.
To enhance your understanding of the class, let's create another class and a class with a wide range of shapes.
Let's take a look at the rectangle. What are the attributes of the rectangle in two aspects? What are the functions of rectangles?

Class rectangle {// The length of the attribute rectangle of the rectangle; the width of the rectangle; // The perimeter of the rectangle using the rectangle method; the area of the rectangle ;}
 

If you use this class to create multiple rectangle objects, each rectangle object has its own length and width, you can find its own week.
Long and area.
Class declaration here !!

The above is the content of php object-oriented tutorial 2. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.