Object Oriented Programming (OOP, object-oriented programming) is a computer programming architecture. One of the basic principles of OOP is that a computer program is composed of a single unit or object that can function as a subroutine. OOP achieves the three goals of software engineering: reusability, flexibility, and scalability. In order to implement the overall operation, each object can receive information, process data and send information to other objects. Object-oriented has always been a hot topic in the field of software development. First of all, object-oriented is in line with the general rules of how humans look at things. Secondly, the use of object-oriented methods allows each part of the system to perform its duties and perform its duties. This opens the door for programmers to write code that is simpler, easier to maintain, and more reusable. Some people say that PHP is not a true object-oriented language, and this is true. PHP is a hybrid language, you can use OOP or traditional procedural programming. However, for large projects, you may need to use pure OOP to declare classes in PHP and only use objects and classes in your project. I won’t go into detail about this concept, because the main reason why many friends stay away from object-oriented programming is that they can’t understand it when they come into contact with it, so they don’t want to learn it. Let the readers understand the concept after reading the overall content.
The concept of class: A class is a collection of objects with the same properties and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: properties and services. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attribute description and service description.
The concept of object: An object is an entity used to describe objective things in the system. It is a basic unit that constitutes the system. An object consists of a set of properties and a set of services that operate on the set of properties. From a more abstract perspective, an object is an abstraction of something in the problem domain or implementation domain. It reflects the information that the thing needs to save and the role it plays in the system; it is a set of attributes and the right to control these attributes. An encapsulation of a set of services that perform operations. The objective world is composed of objects and the connections between objects.
The relationship between classes and objects is like the relationship between molds and castings. The instantiation result of a class is an object, and the abstraction of a type of object is a class. A class describes a group of objects that have the same characteristics (properties) and the same behavior (methods).
The above are probably their definitions. Maybe you are new to object-oriented. Don’t be confused by the concepts. Let me give you an example. If you go to Zhongguancun and want to buy a few assembled PCs, What is your first step when you get there? The installation engineer will sit with you and complete an installation configuration list with you based on the information you provided. This configuration list can be imagined as a class. It is just a Paper, but it records the information of the PC you want to buy. If you use this configuration list to buy 10 machines, then these 10 machines are all composed according to this configuration list, so these 10 machines are of the same type. , can also be said to be of the same type. So what is an object? The instantiation result of a class is an object. The machine configured (instantiated) using this configuration sheet is an object, an entity that we can operate. 10 machines, 10 objects. Each machine is independent, which only means that they are of the same type. Any action taken on one of the machines will not affect the other 9 machines. However, when I modify the class, I add one or less to this configuration list. An accessory, then all nine installed machines have changed. This is the relationship between classes and objects (the instantiation result of a class is an object).
Not to mention his concept, if you want to build a computer classroom, you must first have a room with N computers, N tables, N chairs, whiteboards, projectors, etc. These are What, we just said, these are objects, entities that can be seen. It can be said that the units of this computer classroom are these entity objects. They together make up this computer classroom. So we are doing programs, which What does it have to do with object-oriented? Developing a system program is similar to building a computer classroom. You abstract each independent functional module into a class and form an object. The system is composed of multiple objects. These objects can receive information, process data and send data to other objects. Information and so on interact. It constitutes an object-oriented program.
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 Basic program syntax definition rules can be made, so what’s the difficulty? How many classes and objects should be used in a project, where the class should be defined, what kind of class is defined, how many objects are instantiated by this class, how many attributes are there in the class, how many methods are there, etc. This requires readers to analyze, design and summarize practical problems in actual development.
Class definition: class class name { }
Use a keyword class followed by a class name you want and a pair of curly brackets. In this way, the structure of a class is defined. Just write the code in it. But what should be written 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 the installation configuration sheet we mentioned above, what is written on the installation configuration sheet? The machine has everything. 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 our description of a person. Now let’s summarize, all objects we use classes to describe are similar. From the description of the person above It can be seen that making a class is divided into two parts from a definition point of view. The first is a static description, and the second is a dynamic description. The static description is what we call attributes, as we saw above , the person’s name, gender, age, height, weight, phone number, home address, etc. Dynamically speaking, it is the function of a human object. For example, this person can drive, speak English, use a computer, etc. When abstracted into a program, we write the dynamic as a function or method. Functions and methods are 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 people{
Member attributes: name, gender, age, height, weight, phone number, home address
Membership methods: Can drive, can speak English, can use a computer
}
By using the keyword "var" in the class definition to declare a variable, the attributes of the class are created. Although the initial value can be given when declaring the member attribute, the initial value is given to the member attribute when declaring the class. It is not necessary. For example, if you assign a person's name to "Zhang San", then use this class instance to create dozens of people, and these dozens of people will be named Zhang San, so it is not necessary. We give it after the object is created from the instance. The initial value of the member attribute is enough. Such as: var $somevar;
<?php class Person { //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //下面是人的成员方法 function say() //这个人可以说话的方法 { echo "这个人在说话"; } function run() //这个人可以走路的方法 { echo "这个人在走路"; } } ?>
The above is a declaration of a class, a class declared in terms of attributes and methods. However, it is best not to give initial values to member attributes when declaring them, because the class we make is a description information that will be used in the future. It instantiates objects. For example, if it instantiates 10 human objects, then each of these 10 people will have a different name, gender, and age, so it is best not to assign initial values to member attributes here, but Values are assigned to each object separately.
Using the same method, you can create the class you want. As long as you can use attributes and methods to describe entities, you can define them as classes and instantiate objects.
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. Think about it from two aspects. Rectangle What are the attributes of ? What are the functions of a rectangle?
class 矩形 { //矩形的属性 矩形的长; 矩形的宽; //矩形的方法 矩形的周长; 矩形的面积; }
<?php class Rect { var $kuan; var $gao; function zhouChang() { 计算矩形的周长; } function 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 and area.
We said above that the unit of an object-oriented program is an object, but objects are instantiated through classes. Now that our class has been declared, the next step is to instantiate the object. After defining the class, we use the new keyword to generate an object.
<?php class Person { //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //下面是人的成员方法 function say() //这个人可以说话的方法 { echo "这个人在说话"; } function run() //这个人可以走路的方法 { echo "这个人在走路"; } } $p1=new Person(); $p2=new Person(); $p3=new Person(); ?>
$p1=new Person();
这条代码就是通过类产生实例对象的过程,$p1就是我们实例出来的对象名称,同理,$p2, $p3也是我们实例出来的对象名称,一个类可以实例出多个对象,每个对象都是独立的,上面的代码相当于实例出来3个人来,每个人之间是没有联系的,只能说明他们都是人类,每个人都有自己的姓名,性别和年龄的属性,每个人都有说话和走路的方法,只要是类里面体现出来的成员属性和成员方法,实例化出来的对象里面就包含了这些属性和方法。
对像在PHP里面和整型、浮点型一样,也是一种数据类,都是存储不同类型数据用的,在运行的时候都要加载到内存中去用, 那么对象在内存里面是怎么体现的呢?内存从罗辑上说大体上是分为4段, 栈空间段、堆空间段、代码段、 初始化静态段,程序里面不同的声明放在不同的内存段里面,栈空间段是存储占用相同空间长度并且占用空间小的数据类型的地方,比如说整型1, 10, 100, 1000, 10000, 100000等等,在内存里面占用空间是等长的,都是64位4个字节。 那么数据长度不定长,而且占有空间很大的数据类型的数据放在那内存的那个段里面呢?这样的数据是放在堆内存里面的。栈内存是可以直接存取的,而堆内存是不可以直接存取的内存。对于我们的对象来数就是一种大的数据类型而且是占用空间不定长的类型,所以说对象是放在堆里面的,但对象名称是放在栈里面的,这样通过对象名称就可以使用对象了。
$p1=new Person();
对于这个条代码, $p1是对象名称在栈内存里面,new Person()是真正的对象是在堆内存里面的。
每个在堆里面的实例对象是存储属性的,比如说,现在堆里面的实例对象里面都存有姓名、性别和年龄。每个属性又都有一个地址。$p1=new Person();等号的右边$p1是一个引用变量,通过赋值运算符“=”把对象的首地址赋给“$p1”这个引用变量,所以$p1是存储对象首地址的变量,$p1放在栈内存里边,$p1相当于一个指针指向堆里面的对象,所以我们可以通过$p1这个引用变量来操作对象,通常我们也称对象引用为对象。
上面看到PHP对象中的成员有两种一种是成员属性,一种是成员方法。对象我们已经可以声明了,$p1=new Person();怎么去使用对象的成员呢?要想访问对象中的成员就要使用一个特殊的操作符“->”来完成对象成员的访问:
对象->属性 $p1->name; $p2->age; $p3->sex;
对象->方法 $p1->say(); $p2->run();
<?php class Person { //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //下面是人的成员方法 function say() //这个人可以说话的方法 { echo "这个人在说话"; } function run() //这个人可以走路的方法 { echo "这个人在走路"; } } $p1=new Person(); //创建实例对象$p1 $p2=new Person(); //创建实例对象$p2 $p3=new Person(); //创建实例对象$p3 //下面三行是给$p1对象属性赋值 $p1->name=”张三”; $p1->sex=”男”; $p1->age=20; //下面三行是访问$p1对象的属性 echo “p1对象的名子是:”.$p1->name.”<br>”; echo “p1对象的性别是:”.$p1->sex.”<br>”; echo “p1对象的年龄是:”.$p1->age.”<br>”; //下面两行访问$p1对象中的方法 $p1->say(); $p1->run(); //下面三行是给$p2对象属性赋值 $p2->name=”李四”; $p2->sex=”女”; $p2->age=30; //下面三行是访问$p2对象的属性 echo “p2对象的名子是:”.$p2->name.”<br>”; echo “p2对象的性别是:”.$p2->sex.”<br>”; echo “p2对象的年龄是:”.$p2->age.”<br>”; //下面两行访问$p2对象中的方法 $p2->say(); $p2->run(); //下面三行是给$p3对象属性赋值 $p3->name=”王五”; $p3->sex=”男”; $p3->age=40; //下面三行是访问$p3对象的属性 echo “p3对象的名子是:”.$p3->name.”<br>”; echo “p3对象的性别是:”.$p3->sex.”<br>”; echo “p3对象的年龄是:”.$p3->age.”<br>”; //下面两行访问$p3对象中的方法 $p3->say(); $p3->run(); ?>
从上例中可以看出只是对象里面的成员就要使用对象->属性、对象->方法形式访问,再没有第二种方法来访问对象中的成员了。
现在我们知道了如何访问对象中的成员,是通过“对象->成员”的方式访问的,这是在对象的外部去访问对象中成员的形式,那么如果我想在对象的内部,让对象里的方法访问本对象的属性,或是对象中的方法去调用本对象的其它方法这时我们怎么办?因为对象里面的所有的成员都要用对象来调用,包括对象的内部成员之间的调用,所以在PHP里面给我提供了一个本对象的引用$this, 每个对象里面都有一个对象的引用$this来代表这个对象,完成对象内部成员的调用, this的本意就是“这个”的意思, 上面的实例里面,我们实例化三个实例对象$P1、 $P2、 $P3,这三个对象里面各自存在一个$this分别代表对象$p1、$p2、$p3 。
通过上图我们可以看到,$this就是对象内部代表这个对象的引用,在对象内部和调用本对象的成员和对象外部调用对象的成员所使用的方式是一样的。
$this->属性 $this->name; $this->age; $this->sex;
$this->方法 $this->say(); $this->run();
修改一下上面的实例,让每个人都说出自己的名字,性别和年龄:
<?php class Person { //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //下面是人的成员方法 function say() //这个人可以说话的方法 { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } function run() //这个人可以走路的方法 { echo "这个人在走路"; } } $p1=new Person(); //创建实例对象$p1 $p2=new Person(); //创建实例对象$p2 $p3=new Person(); //创建实例对象$p3 //下面三行是给$p1对象属性赋值 $p1->name="张三"; $p1->sex="男"; $p1->age=20; //下面访问$p1对象中的说话方法 $p1->say(); //下面三行是给$p2对象属性赋值 $p2->name="李四"; $p2->sex="女"; $p2->age=30; //下面访问$p2对象中的说话方法 $p2->say(); //下面三行是给$p3对象属性赋值 $p3->name="王五"; $p3->sex="男"; $p3->age=40; //下面访问$p3对象中的说话方法 $p3->say(); ?>
分析一下这个方法:
function say() //这个人可以说话的方法
{
echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."
";
}
在$p1、$p2和$p3这三个对象中都有say()这个方法,$this分别代表这三个对象, 调用相应的属性,打印出属性的值,这就是在对象内部访问对象属性的方式, 如果相在say()这个方法里调用run()这个方法也是可以的,在say()这个方法中使用$this->run()的方式来完成调用。
大多数类都有一种称为构造函数的特殊方法。当创建一个对象时,它将自动调用构造函数,也就是使用new这个关键字来实例化对象的时候自动调用构造方法。
构造函数的声明与其它操作的声明一样,只是其名称必须是__construct( )。这是PHP5中的变化,以前的版本中,构造函数的名称必须与类名相同,这种在PHP5中仍然可以用,但现在以经很少有人用了,这样做的好处是可以使构造函数独立于类名,当类名发生改变时不需要改相应的构造函数名称了。为了向下兼容,如果一个类中没有名为__construct( )的方法,PHP将搜索一个php4中的写法,与类名相同名的构造方法。
在一个类中只能声明一个构造方法,而是只有在每次创建对象的时候都会去调用一次构造方法,不能主动的调用这个方法,所以通常用它执行一些有用的初始化任务。比如对成属性在创建对象的时候赋初值。
<? //创建一个人类 class Person { //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //定义一个构造方法参数为姓名$name、性别$sex和年龄$age function __construct($name, $sex, $age) { //通过构造方法传进来的$name给成员属性$this->name赋初使值 $this->name=$name; //通过构造方法传进来的$sex给成员属性$this->sex赋初使值 $this->sex=$sex; //通过构造方法传进来的$age给成员属性$this->age赋初使值 $this->age=$age; } //这个人的说话方法 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } } //通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄 $p1=new Person(“张三”,”男”, 20); $p2=new Person(“李四”,”女”, 30); $p3=new Person(“王五”,”男”, 40); //下面访问$p1对象中的说话方法 $p1->say(); //下面访问$p2对象中的说话方法 $p2->say(); //下面访问$p3对象中的说话方法 $p3->say(); ?>
与构造函数相对的就是析构函数。析构函数是PHP5新添加的内容,在PHP4中没有析构函数。析构函数允许在销毁一个类之前执行的一些操作或完成一些功能,比如说关闭文件,释放结果集等,析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行,也就是对象在内存中被销毁前调用析构函数。与构造函数的名称类似,一个类的析构函数名称必须是__destruct( )。析构函数不能带有任何参数。
<? //创建一个人类 class Person { //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //定义一个构造方法参数为姓名$name、性别$sex和年龄$age function __construct($name, $sex, $age) { //通过构造方法传进来的$name给成员属性$this->name赋初使值 $this->name=$name; //通过构造方法传进来的$sex给成员属性$this->sex赋初使值 $this->sex=$sex; //通过构造方法传进来的$age给成员属性$this->age赋初使值 $this->age=$age; } //这个人的说话方法 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } //这是一个析构函数,在对象销毁前调用 function __destruct() { echo “再见”.$this->name.”<br>”; } //通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄 $p1=new Person(“张三”,”男”, 20); $p2=new Person(“李四”,”女”, 30); $p3=new Person(“王五”,”男”, 40); //下面访问$p1对象中的说话方法 $p1->say(); //下面访问$p2对象中的说话方法 $p2->say(); //下面访问$p3对象中的说话方法 $p3->say(); ?>
封装性是面向对象编程中的三大特性之一,封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义:1.把对象的全部属性和全部服务结合在一起,形成一个不可分割的独立单位(即对象)。2.信息隐蔽,即尽可能隐蔽对象的内部细节,对外形成一个边界〔或者说形成一道屏障〕,只保留有限的对外接口使之与外部发生联系。
封装的原则在软件上的反映是:要求使对象以外的部分不能随意存取对象的内部数据(属性),从而有效的避免了外部错误对它的"交叉感染",使软件错误能够局部化,大大减少查错和排错的难度。
用个实例来说明吧,假如某个人的对象中有年龄和工资等属性,像这样个人隐私的属性是不想让其它人随意就能获得到的,如果你不使用封装,那么别人想知道就能得到,但是如果你封装上之后别人就没有办法获得封装的属性,除非你自己把它说出去,否则别人没有办法得到。
再比如说,个人电脑都有一个密码,不想让其它人随意的登陆,在你的电脑里面拷贝和粘贴。还有就是像人这个对象,身高和年龄的属性,只能是自己来增涨,不可以让别人随意的赋值等等。
使用private这个关键字来对属性和方法进行封装:
原来的成员: var $name; //声明人的姓名 var $sex; //声明人的性别 var $age; //声明人的年龄 function run(){…….} 改成封装的形式: private $name; //把人的姓名使用private关键字进行封装 private $sex; //把人的性别使用private关键字进行封装 private $age; //把人的年龄使用private关键字进行封装 private function run(){……} //把人的走路方法使用private关键字进行封装 注意:只要是成员属性前面有其它的关键字就要去掉原有的关键字“var”。
通过private就可以把人的成员(成员属性和成员方法)封装上了。封装上的成员就不能被类外面直接访问了,只有对象内部自己可以访问;
私有的成员是不能被外部访问的, 因为私有成员只能在本对象内部自己访问,比如,$p1这个对象自己想把他的私有属性说出去,在say()这个方法里面访问了私有属性,这样是可以。(没有加任何访问控制,默认的是public的,任何地方都可以访问)
因为构造方法是默认的公有方法(构造方法不要设置成私有的),所以在类的外面可以访问到,这样就可以使用构造方法创建对象, 另外构造方法也是类里面的函数,所以可以用构造方法给私有的属性赋初值。Say()的方法是默认公有的, 所以在外面也可以访问的到, 说出他自己的私有属性。
From the above example, we can see that private members can only be used inside the class and cannot be directly accessed by outside the class. However, they have permission to access inside the class, so sometimes we need to Assigning and reading private properties outside the class means providing some accessible interfaces outside the class. In the above example, the constructor method is a form of assignment, but the constructor method only assigns values when the object is created. If We already have an existing object and want to assign a value to this existing object. At this time, if you also use the constructor method to pass a value, then a new object will be created, not the existing object. . Therefore, we need to make some interfaces for private attributes that can be accessed externally. The purpose is to change and access the value of the attribute when the object exists. However, it should be noted that this can only be done for attributes that need to be changed externally. Properties that do not want to be accessed by the outside do not have such an interface, so that the purpose of encapsulation can be achieved. All functions are completed by the object itself, providing as few operations as possible to the outside world.