Home >php教程 >php手册 >1.1 Object-oriented

1.1 Object-oriented

WBOY
WBOYOriginal
2016-08-20 08:47:361069browse

Object-oriented is a programming idea and has nothing to do with specific languages. C, java, JavaScript, and php can all carry out object-oriented development with their own style.

A class is an encapsulation of the attributes and behaviors of a type of thing. Why do we need a class? First of all, we have to think about what would happen if there were no classes? If there were no classes, assuming we want to represent an ordinary bicycle, we would think The number of wheels is 2, and the price is... Then we have to represent a bicycle that a child can ride, and we think about the number of wheels, 4, and the price... In this way, as long as there is a bicycle, we will all think that it should have wheels. The attribute of quantity will also have the attribute of price...how many types of bicycles there are, we have to think about what attributes and functions they have and how many times.

<strong>array</strong>(<strong>'</strong><strong>名字</strong><strong>'</strong>=><strong>'</strong><strong>普通自行车</strong><strong>'</strong>,<strong>'</strong><strong>轮子数量</strong><strong>'</strong>=>2,<strong>'</strong><strong>价格</strong><strong>'</strong>=><strong>'...'</strong>);<br>
<strong>array</strong>(<strong>'</strong><strong>名字</strong><strong>'</strong>=><strong>'</strong><strong>儿童自行车</strong><strong>'</strong>,<strong>'</strong><strong>轮子数量</strong><strong>'</strong>=>4,<strong>'</strong><strong>价格</strong><strong>'</strong>=><strong>'...'</strong>);

What if we have classes? Let’s say we have a bicycle class:

<strong>class </strong>自行车{<br>
    <strong>public </strong><strong>$</strong><strong>名字</strong>;<br>
    <strong>public </strong><strong>$</strong><strong>轮子数量</strong>;<br>
    <strong>public </strong><strong>$</strong><strong>价格</strong>;<br>
    <strong>public function </strong>骑(){<br>
        <strong>echo </strong><strong>'</strong><strong>骑</strong><strong>'</strong>.$this-><strong>名字</strong>;<br>
    }<br>
}

Now as long as it is a bicycle, you don’t have to think about its attributes and functions at first. Just instantiate the bicycle class directly. The attributes have been written in the class, and the functions are also there. You don’t need to think about them one by one.

$自行车对象1=<strong>new </strong>自行车();<br>
$自行车对象1-><strong>名字</strong>=<strong>'</strong><strong>普通自行车</strong><strong>'</strong>;<br>
$自行车对象1-><strong>轮子数量</strong>=2;<br>
<br>
$自行车对象2=<strong>new </strong>自行车();<br>
$自行车对象2-><strong>名字</strong>=<strong>'</strong><strong>儿童自行车</strong><strong>'</strong>;<br>
$自行车对象2-><strong>轮子数量</strong>=4;

Here, the class feels more like a constraint, or regulation, which stipulates the attributes and functions that all bicycles will have. It is impossible to say that a snappy property will appear after instantiating the bicycle class. Because when you instantiate At the same time, the initial attributes have been limited.

Now the question comes again. There can be many instantiated objects of a class. How can these objects be distinguished from each other?

Differenced by different values ​​​​of the same attribute.

For example, the name of object 1 and the name of object 2 above are different, and the number of wheels is also different. This is their difference.

The question comes again, can we use methods to distinguish?

Can’t

In PHP, all objects share class methods. That is to say, as long as the object uses a method, this method is taken from the class, and the object itself does not have it. What the object itself does is just assign values ​​​​to attributes. It’s just different from other objects.

Objects and Arrays:

Object: attribute name = attribute value

array:key=>value

The structures of arrays and objects in Php are very similar after serialization, as shown in the figure:

The second line is the serialization result of the object. You can see that there are three words "bicycle" in front of the serialization result of the object. These three words indicate that the object belongs to the bicycle class. After deserialization, it can also Find its class. The reason why you cannot see the method information in the serialization result is because the method belongs to the class. After deserialization, you can find the corresponding class through the information of `bicycle`, and then call the method in the class. .

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