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 is very simple:
Class my_class {}
What is the class? Many people say what black box, I call it as 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:
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 of this, it is to let all functions in this class can access it, and it distinguish between a function of functions, it is also an externally accessible and controlled this variable, I will then talk about external How to 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:
Class my_class {var $ usrname = "deep";
OK, define a public variable, next to define a function (that is, the so-called "method"):
Class my_class {var $ usrname = "deep";
Function show_username () {}}
This definition function has no difference in the form of a normal definition function. Just, define a function of printing $ usrname:
Class my_class {var $ usrname = "deep";
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? ~~
The shape of the function belt, don't explain it? This function function is to print the value of the confessed value, that is, if:
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:
$ 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:
$ Name-> show_username ("Sterling of the pig"); dizzy, 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: How did 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:
$ 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:
Class my_class {var $ usrname = "deep";
Function show_username () {echo $ this-> username;}}
Wow, not, don't you have? 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. Visit? Is this professional? In fact, use $ this-> username to replace var $ username, $ this is used to explain that it is public, accessible, external, such as other variables or functions).
Try it:
$ 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.
$ 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:
$ 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.
But only one name is also too interesting, let's say something, let's expand this class, create a function called Welcome:
Class my_class {var $ usrname = "deep";
Function show_username () {Echo $ this-> username;}
Function welcome () {}}
Well, what function is good? Simply click, you will be "welcome" in front of the name.
Class my_class {var $ usrname = "deep";
Function show_username () {echo $ this-> username;} function welcome () {echo "Welcome"; $ this-> show_username ();}}
I saw $ this second time? And the last time, $ this-> show_username (); what is it used? Point to a function in the class, 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:
$ 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:
$ Name-> username = "The pig head is deep";
Next print, welcome language:
$ 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.