A entry-entry class article (written when bored)

zhaozj2021-02-12  144

A entry-entry class article (written when bored)

A entry-entry class article (written when bored)

Author: ChingKwan Source: PHPX

Just in the big browsing of the home update article about Class's articles (referring to PHP: //www.phpe.net/articles/389.shtml), very good, suggestion. The exploration of the class ~~ 俺 uses half a year to understand the role and implementation of the class. Mainly there is no article that makes me understand (there have been no more OO before it.). In my point of view, the class in PHP is said, and the language used to express is a non-formal language, and it cannot be determined whether it is correct. Establish a class very simple.

PHP code:

Class my_class {}

What is the class? Many people are any black box, I call it a separate whole. We only know the name of the class, and don't know what is in it. So how do you use this class? First of all: To know if it defines a public variable - the professional term is called "attribute." Second: Do you know what function is defined - it is called "method" in the professional term. I have been confused by these major terms, so I just ignore it. How do I have a common variable in the class? Very simple, let's expand the MY_CLASS class

PHP code:

Class my_class {var $ usrname;}

It is very simple to see, we define a public variable, just consist of Var space ordinary variable name. What is it used? Consider the function, if we want to access the variables outside the function, isn't it to GLOBAL? This is true for this, it is to let all functions in this class can access it, and it distinguish between a function of the function, it is an exterior of the class or access this variable, I will then talk about how external access it . There is also a difference that cannot be assigned to this variable with a complex statement (specific, etc., you can see the rules after the class). Give it a default value

PHP code:

Class my_class {var $ usrname = "deep";

OK, define a common variable, next to define a function (that is, the so-called "method").

PHP code:

Class my_class {var $ username = "deep space"; function show_username () {}}

This definition function has no difference in the form of a normal definition function. Just, define a function of printing $ usrname:

PHP code:

Class my_class {var $ username = "deep empty"; function show_username ($ usrname) {echo $ usrname;}}

It may be fascinating here, huh, huh, the most critical is here, see it clear. There are now three $ usrname. Which one is it ?~~~ Don't explain it? This function function is to print the value of the row, that is, if:

PHP code:

Show_username ("deep space");

Then it will print "deep space", just as simple. How to access this function? It is definitely not that I have so-saying it directly show_username ("Sterling of the pig");, don't worry, there is a set of classes. As follows: PHP code:

$ Name = new my_class ();

This initializes the type of my_class above, and assigns this object to a variable $ name, you can understand this, this variable represents the entire class, huh, huh. Use the functions in the class:

PHP code:

$ Name-> show_username ("deep space");

Dizziness, why is this complicated? Also arrow? In fact, it is very imageable. Originally gave the class to the variable $ name? That is, $ name represents this class and then uses an arrow to point to show_username this function in the class. It is such a simple, that is, this function is in this class, not other functions - you understand that it means a difference, huh, huh. Try it, print the four words of "deep space". Why are you so complicated? Is it possible to use a function? I said, you will certainly see the benefits, we will continue to expand. There is also a question: "What is the" public variable "just said? Why does this function do not automatically receive the default value in this public variable VAR $ username? That is, if I use:

PHP code:

$ Name-> show_username ($ usrname);

What do you have? The answer is not any output. Because you don't give the parameter $ username a value. So how do you use this public variable? Let's modify this class:

PHP code:

Class my_class {var $ username = "deep empty"; function show_username () {echo $ this-> username;}}

Wow, not, don't you have a face shape? There are more $ this->, dizzy, huh, huh. In fact, this is also a biggest convenience of the class. $ this role: Access a public variable, or a function in the class. access? Is this professional? In fact, use $ this-> username instead of Var $ usrname, $ this is used to explain it is public. It can be accessed, and the function is outside. Try it:

PHP code:

$ Name-> show_username ();

I saw it, I finally printed two words "deep and empty", Wahaha. I don't print "deep space", I want to print "deep space of pig", what should I do? Very simple, we re-value this public variable. I took you.

PHP code:

$ Name-> username = "The pig head is deep";

Can this understand what it means? $ Name-> username is a common variable in the class. I don't need to explain it. Let's print again.

PHP code:

$ Name-> show_username ();

Haha, I finally printed "the pig's head is deep". Yes, it is very convenient, you can also modify the printed value without the shape. However, only one name is also too interesting, let's say something, come to expand this class, create a function called Welcome: PHP code:

Class my_class {var $ usrname = "deep empty"; function show_username () {Echo $ this-> username;} function welcome () {}}

Well, what function is good? Simply click, you will be "welcome" in front of the name.

PHP code:

Class my_class {var $ username = "deep empty"; function show_username () {Echo $ this-> username;} function welcome () {echo "welcome"; $ this-> show_username ();}}

I saw $ this second time? $ this-> show_username (); what is your use? In fact, it is called show_username, with $ this to indicate this function in the class and the Welcome function is parallel, not elsewhere (such as a Welcome function). The function of the Welcome function is very simple. First print two words "welcome", then take the show_username function, print the name. Try this function:

PHP code:

$ Name-> Welcome ();

I saw it, and I printed "Welcome Dead". But I want to print "Welcome to the pig's head", what should I do? I have served you, we give the public variable Var $ username a value:

PHP code:

$ Name-> username = "The pig head is deep";

Next print, welcome language:

PHP code:

$ Name-> Welcome ();

Hey, finally print "Welcome to the deep air". how about it? Understand the usage of the class? The advantage is to call any of the functions in the class, as long as it is pointed out with $ this, you can change the value of a common variable, you can use this common variable in the function in the class. ......... I have went, and it's the app to wait for you.

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

New Post(0)