1.1 object-oriented, envi5.1 object-oriented classification

Source: Internet
Author: User

1.1 object-oriented, envi5.1 object-oriented classification

Object-Oriented Programming is irrelevant to specific languages. c, java, JavaScript, and php can all be used for object-oriented development with their own styles.

Class is the encapsulation of all attributes and behaviors of a class of things. Why do we need a class? First, let's think about what will happen if there is no class? If there is no category, if we want to present a normal bicycle, we will think about the number of wheels, the price... Then we want to say that a child is riding a bicycle, and we want to think about four wheels, the price... In this way, as long as there is a bicycle, we will think that it should have the property of "Number of wheels" and the property of price ..... We need to think about the attributes and functions of a bicycle.

Array('Name'=>'Ordinary bicycle','Wheel quantity'=> 2,'Price'=>'...');
Array('Name'=>'Children's bicycles','Wheel quantity'=> 4,'Price'=>'...');

 

What if there is a class? Suppose we have a bicycle class:

ClassBicycle {
Public$Name;
Public$Wheel quantity;
Public$Price;
Public functionRide (){
Echo'Ride'. $ This->Name;
}
}

Now, as long as it is a bicycle, you don't have to think about its attributes and functions. just instantiate the bicycle class directly. attributes have been written in the class, and functions are available. you don't have to think about it any more.

$ Bike object 1 =NewBicycle ();
$ Bike object 1->Name='Ordinary bicycle';
$ Bike object 1->Wheel quantity= 2;

$ Bike object 2 =NewBicycle ();
$ Bike object 2->Name='Children's bicycles';
$ Bike object 2->Wheel quantity= 4;

Here, classes are more like constraints or rules that define the attributes and functions of all bicycles. it cannot be said that there is a snap property after the bicycle class is instantiated. because the initial attribute is already limited while you instantiate it.

 

Now the question is, there can be many class instantiation objects. How can these objects be different from each other?

Different values of the same attribute are used to differentiate.

For example, the names of object 1 are different from those of object 2, and the number of wheels is also different. This is the difference between them.

 

The problem comes again. Can I use a different method?

No

All objects in php share classes. that is to say, as long as the object uses the method, this method is obtained from the class, and the object itself does not. what an object does is assign values to attributes to distinguish it from other objects.

 

Objects and arrays:

Object: property name = property value

Array: Key => Value

Arrays and objects in Php have similar structures after serialization ,:

The serialization result of the second behavior object shows that the object's serialization result is preceded by the word "bicycle", which indicates that the object belongs to the bicycle class. after deserialization, it can also find its class. the reason why the method information is not visible in the serialization result is that the method belongs to the class. After deserialization, find the corresponding class through the 'bicycle 'information, and then call the method in the class.

 

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.