PHP Object-Oriented tutorial 2

Source: Internet
Author: User
4. How do I abstract a class?
As described above, object-oriented programs are objects, but objects are instantiated by class, so
The first thing we need to do is to declare the class, it is easy to make a class, as long as you master the Basic program syntax definition rules can be
To do, then the difficulty is there? How many classes to use for a project, how many objects to use, where to define the class, define
What kind of class, how many objects are instantiated in this class, how many properties there are in the class, how many methods are there, and so on, which requires
To 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, plus a pair of curly braces, such that the structure of a class
It is defined, as long as the code is written in it, but what does it say? What can I write? How to write is a complete
What about the class? It says that using a class is to let it instantiate an object for us, and that's what you want.
It's like, like the one on the list we're talking about, what's on the machine you're faking. For example, a person is a
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 the objects we use to describe the class are similar, from the description of the above can be seen, to make a class, from a fixed
The angle of righteousness is divided into two parts, the first is the static description, the second is the dynamic description, the static description is what we say
attributes, such as what we see above, people's names, gender, age, height, weight, phone, home address and so on. On the dynamic Also
Is the function of this object, such as the person can drive, can speak English, can use computer and so on, abstract into a program,
We write the dynamic as a function or a method, and the functions and methods are the same. So, all classes are from the properties and methods of these two
Aspect to write, the attribute is also called the member property of the class, and the method is called the member method of this class.

Class Person {member properties: Name, gender, age, height, weight, phone, home Address member method: can drive, speak English, can use the computer}

Property:
You declare a variable by using the keyword "var" in the class definition, that is, the properties of the class are created, although the declared member property
Can be given an initial value, but it is not necessary to give the initial value of the member property when declaring the class, for example, to
is to put 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
It is not necessary that we give the member property the initial value after the instance is out of the object.
Such as:

var $somevar; Method (member function): A method that creates a class by declaring a function in the class definition. such as: function somefun (parameter list) {...} <?phpclass person{//Below is a member of the person attribute var $name;//person's name Var $sex;//gender var $age;//person's age//The following is the person's member method function say ()//This person can speak the way {echo "This person is talking";} function run ()//This person can walk the way {echo "This person is walking";}}? >

The above is a class declaration, a class declared from a property and a method, but the member property is best when declared
Don't give the initial value because the person we're doing this class is a descriptive information that will be instantiated with it in the future, such as instantiation
10 Person object, then these 10 people, each person's name, gender, age are different, so better not in this
The place assigns the initial value to the member property, but assigns each object the values individually.
The same method can be used to make the class you want, as long as you can use attributes and methods can be described by the entity can be defined as
class to instantiate the object.
In order to strengthen your understanding of the class, we do a class, a shape of the class, the shape of a wide range of points, we do a
Rectangular bar, first analysis, think about the two aspects of analysis, what are the properties of the rectangle? What are the functions of a rectangle?

Class Rectangle {//Rectangle's property rectangle's length, rectangle width,//Rectangle's method Rectangle's circumference, rectangular area,}<?phpclass Rect{var $kuan, var $gao; function Zhouchang () {Calculates the perimeter of the rectangle;} function Mianji () {Calculates the area of the rectangle;}}?>

If you use this class to create multiple rectangular objects, each Rectangle object has its own length and width, and you can find your own week
Length and area.
Declaration of class let's get here!!

The above is the PHP object-oriented tutorial 2 content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.