Home  >  Article  >  Backend Development  >  php object-oriented tutorial 2

php object-oriented tutorial 2

黄舟
黄舟Original
2016-12-29 10:48:521413browse

4. How to abstract a class?
As mentioned above, the unit of object-oriented program is the object, but the object is instantiated by the class, so
The first thing we have to do is how to declare the class. It is easy to make a class. , as long as you master the basic programming grammar definition rules, you can
do it, so what is the difficulty? How many classes and objects should be used in a project? Where the class needs to be defined, define
what kind of class it is, how many objects this class instantiates, how many attributes and how many methods there are in the class. Wait, this requires readers to analyze, design and summarize practical problems in actual development.
Definition of class:

class 类名{
}

Use a keyword class followed by a class name you want and a pair of braces, so that the structure of a class

is Once it’s defined, you just need to write code in it, but what should you write in it? What can I write? How to write a complete
class? As mentioned above, the purpose of using a class is to instantiate objects for us to use. This requires knowing what kind of object you want. Like what we mentioned above on an installation configuration sheet, The machine you put on is whatever it is. For example, a person is a target. How do you recommend a person you like to your leader? Of course, the more detailed the better:
First, you will introduce the person's name, gender, age, height, weight, phone number, home address, etc.
Then, you have to introduce what this person can do, whether he can drive, speak English, use a computer, etc.
As long as you introduce more, others will know more about this person. This is how we describe a person. Now let’s summarize
. All objects we use classes to describe are similar. From the above You can see from the human description that when you make a class, it is divided into two parts from the perspective of definition. The first is a static description, and the second is a dynamic description. The static description is what we call it.
Attributes, as we saw above, the person's name, gender, age, height, weight, phone number, home address, etc. Dynamically speaking, it is also the function of this human object. For example, this person can drive, speak English, can use a computer, etc. When abstracted into a program,
we write the dynamic as a function or method, function And the method is the same. Therefore, all classes are written in terms of attributes and methods. Attributes are also called member attributes of this class, and methods are called member methods of this class.

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

Attributes:
Declaring variables by using the keyword "var" in the class definition creates attributes of the class, although it can be given when declaring member attributes
Initial value, but it is not necessary to give initial values ​​to member attributes when declaring a class. For example, if
is to assign a person's name to "Zhang San", then use this class instance to create dozens of people. These dozens Everyone’s name is Zhang San, so
is not necessary. We can just give the initial value of the member attribute after the object is created from the instance.

For example:

var $somevar;
方法(成员函数):
通过在类定义中声明函数,即创建了类的方法。
如: function somefun(参数列表)
{ ... ... }
<?php
class Person
{
//下面是人的成员属性
var $name; //人的名字
var $sex; //人的性别
var $age; //人的年龄
//下面是人的成员方法
function say() //这个人可以说话的方法
{
echo "这个人在说话";
}f
unction run() //这个人可以走路的方法
{
echo "这个人在走路";
}
}
?>

The above is a declaration of a class, a class declared from attributes and methods, but it is best to declare the member attributes
Do not give an initial value, because we The person class is a description information, which can be used to instantiate objects in the future. For example, if
10 personal objects are instantiated, then each of these 10 people will have a different name, gender, and age, so in the end It is best not to assign initial values ​​to member properties in this
place, but to assign values ​​to each object separately.
You can use the same method to make the class you want. As long as you can use attributes and methods to describe the entity, you can define it as a

class to instantiate the object.

In order to strengthen your understanding of classes, let’s make another class, a shape class. The range of shapes is a bit wider, let’s make a
rectangle. Let’s analyze it first and think about it from two aspects. Analysis, what are the properties of a rectangle? What are the functions of a rectangle?

class 矩形
{
//矩形的属性
矩形的长;
矩形的宽;
//矩形的方法
矩形的周长;
矩形的面积;
}
<?php
class Rect
{
var $kuan;
var $gao;
function zhouChang()
{
计算矩形的周长;
}f
unction mianJi()
{
计算矩形的面积;
}
}
?>

If you use this class to create multiple rectangular objects, each rectangular object has its own length and width, and you can calculate its own perimeter
length and area.
Let’s stop here for the declaration of the class! !

The above is the content of PHP object-oriented tutorial 2. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn