Understand the object-oriented object of PHP with a comparison method

xiaoxiao2021-03-06  61

Post original address: Understand the object-oriented object of PHP with comparison [original]

Surfchen

Note a few points:

You must understand basic PHP programming knowledge, you must carefully read each word.

This article is known as php4. PHP5 has a great development in OO, but even a rookie. . Throwing jade. I hope to bring some inspiration to the novice, and I hope that the seniors can give pointers.

This article can only bring you into the door-oriented threshold. To really understand the meaning of OOP in PHP, you have to discover yourself. .

OOP: Object Oriented Programming Object - Oriented Program Design. It can also be called oo.

Class: Information of a certain type of thing and a collection of operation information, that is, attributes and methods

Object: Specific to a certain [individual] of this type of thing.

The following OO part is a class, and $ Surfchen is an [individual].

I will now operate as an example in operating personal information, and operate in a way of object or non-directional objects. This is compared to the difference between OO and nono OO, so that the reader has a preliminary understanding of OO.

First, we create a simple personal information data file info.txt, the content is as follows:

CODE:

======= begin =======

Surfchen 19 Male 13878797321

======== End =======

The first behavior name, two behavior age, three behavior gender, four behavior phone numbers

Now, I will obtain information and modify the age information in a non-OO method. (Please read and understand this non-OO code carefully, this should be easy for you, it is the same functionality. By comparing this code and the OO code, you can easily understand some of the basic things of OO)

CODE:

======= begin =======

$ f = "info.txt"; // Data Save File Name $ file_info = file ($ f); / * Get personal information * / $ name = RTRIM ($ file_info [0]); $ AGE = RTRIM ($ File_Info [1]); $ SEX = RTRIM ($ file_info [2]); $ phone = rtrim ($ file_info [3]); $ phone = "13117601514"; // The item to modify $ total = $ name. "/ n ". $ age. $ sex." / n ". $ phone; $ hand = fopen ($ f," w "); fwrite ($ handle, $ total); // Save modification information Fclose ($ HANDLE);?>

======== End =======

The modified info.txt is

CODE:

======= begin =======

Surfchen 18 Male 13117601514

======== End =======

Use OO method: Code:

======= begin =======

Class Person {/ * constructor, initialize personal information (attribute) * / Function Person ($ file) {$ file_info = file ($ file); $ this-> mfile = $ file; // file name / * get personal Information * / $ this-> mname = RTRIM ($ file_info [0]); // Name $ this-> Mage = RTRIM ($ file_info [1]); // Age $ this-> msex = RTRIM ($ file_info [ 2]); // Gender $ this-> mphone = rtrim ($ file_info [3]); // Phone} / * Modify Name * / Function ChangeName ($ C_NAME) {$ TOTAL = $ C_NAME. "/ N". $ this-> Mage. $ this-> msex. "/ n". $ this-> mphone; $ hand = fopen ($ this-> mfile, "w"); fwrite ($ handle, $ Total); // Save the modification information Fclose ($ hand);} / * Modify age * / Function Changeage ($ c_age) {$ Total = $ THIS-> MNAME. "/ n". $ c_age. "/ n". $ this-> msex. "/ n". $ this-> mphone; $ handle = fopen ($ this-> mfile, "w"); fwrite ($ handle, $ total); // Save the modification information fclose ($ Handle);} / * Modify gender (denatured? -_-) * / Function Changesex ($ c_sex) {$ TOTAL = $ this-> mname. "/ n". $ this-> Mage "/ n". $ c_sex. "/ n". $ this-> mphone; $ handle = fopen ($ this-> mfile, "w"); fwrite ($ handle, $ total); // Save the modification information Fclose ($ Handle);} / * Modify the user's phone * / Function Changephone ($ C_PHONE) {$ TOTAL = $ this-> mname. "/ n". $ this-> Mage. "/ n". $ THIS- > msex. "/ n". $ c_phone; $ hand = fopen ($ this-> mfile, "w"); fwrite ($ handle, $ total); // Save modification information fclose ($ handle);}} / * At this time, you can get an object to obtain and operate by instantiating an object. * / $ Surfchen = new person ("

Info.txt "); // instantiate an object echo" My call ". $ surfchen-> mname.", this year ". $ surfchen-> Mage." "; // list my name and age Echo" mobile phone number ". $ Surfchen-> mphone." "; Echo" To change Unicom's mobile number. . . "; $ Surfchen-> changephone (" 13117601514 "); // Modify mobile phone number?> ======== End =======

$ SURFCHEN-> MNAME means the name of Surfchen's object (as if nonsense, huh, huh), $ SURFCHEN-> Mphone means the phone of Surfchen. (Attributes in the class)

And $ SURFCHEN-> ChangePhone means modifying the phone of Surfchen's object. . (Method in class)

Attributes and methods do not have any dependence. .

Attributes are generally equivalent to the usual variables we say, and the method is substantially equivalent to the function.

The modified info.txt is also

CODE:

======= begin =======

Surfchen 18 Male 13117601514

======== End =======

----------------

From above we can see, using OO method, although the code is much more, you can reuse this class to get and modify anyone's information, without having to modify the specific code each time. . Realize the reuse of the code.

And when you understand Oo, you will find that OO is so close to life. . Ha ha. .

IT talk

转载请注明原文地址:https://www.9cbs.com/read-118761.html

New Post(0)