Author: Deep Space Source: Beyond PHP
In my opinion, the Class used in PHP is an informal language and cannot be determined whether it is correct.
Creating a class is simple:
Class my_class {}
What is the class? Many people refer to it as a black box. Here I call it an independent whole. We only know the class name, but do not know what is in it. So how should we use this class?
First, you need to know whether the public variable is defined in it-technically, it is called "attribute ".
Second, you need to know what function is defined in it-in professional terms, it is called "method ".
I am confused by these technical terms, So I simply ignore it.
How does one define public variables in a class?
It's easy. let's expand the my_class class:
Class my_class
{
Var $ username;
}
The above is simple. We have defined a public variable, which is composed of var + space + common variable names. What is its use? In the function, if we want to access variables outside the function, do we need to make a global operation first? The same is true for this purpose. It allows all functions in this class to access it, and it is different from a function, is the external class can also access and control this variable at any time, I will talk about how to access it externally. Another difference is that you cannot assign values to this variable using complex statements (you can read the rules yourself after understanding the class ).
Give it a default value:
Class my_class
{
Var $ username = "Deep Space ";
}
OK, defines a public variable, and then defines a function (that is, the so-called "method "):
Class my_class
{
Var $ username = "Deep Space ";
Function show_username ()
{
}
}
This definition function is no different from the normal definition function form. It's easy to define a function that prints $ username:
Class my_class
{
Var $ username = "Deep Space ";
Function show_username ($ username)
{
Echo $ username;
}
}
Some people may be confused here. There are now three $ usernames. Which is it ~~
Do you need to explain the parameters in the function? This function prints the value received by the form parameter, that is, if:
Show_username ("pig deep space ");
Then it will print "pig's deep space", which is so simple.
How to access this function? It must not be as direct show_username ("pig deep space") As I said above; now, don't worry, there is a set of classes. As follows:
$ Name = new my_class ();
In this way, the above my_class class is initialized, and this object is assigned to the variable $ Name. You can understand this. This variable represents the entire class.
Use functions in the class:
$ Name-> show_username ("pig deep space ");
Dizzy. Why is it so complicated? Arrow? In fact, it is very visual. I already gave the class $ Name variable, right? That is, $ Name represents this class, and then points an arrow to the show_username function in the class. This is so simple, that is to say, this function is in this class, rather than other functions-you can understand it as a difference, huh, huh.
Try it and print out the four words "pig's Deep Space. Why are you so complicated? Can it be implemented using functions? I said, you certainly cannot see the benefits of this simple process. We will continue to expand.
There is another question: what is the "Public variable" that I just mentioned that is useless at all? Why does this function not automatically receive the default value in the public variable var $ username? That is, if I use:
$ Name-> show_username ($ username );
What will happen? The answer is no output. Because you didn't give the parameter $ username a value.
So how to use this public variable? Let's modify this class:
Class my_class
{
Var $ username = "Deep Space ";
Function show_username ()
{
Echo $ this-> username;
}
}
Wow, isn't it? This time it's not even a parameter? There is another $ this->, and the system is dizzy. In fact, this is also the greatest convenience of the class.
$ This: access a public variable or a function in the class.
Access? So professional? Instead of var $ username, use $ this-> username, $ this indicates that it is public, accessible, and external functions (such as other variables or functions ).
Try:
$ Name-> show_username ();
Now, the word "Deep Space" is printed, Wahaha.
I don't print the word "Deep Space". What should I do if I want to print "pig's deep space? It's easy. Let's re-assign a value to this public variable. Now you are ready.
$ Name-> username = "pig deep space ";
Can this be understood? $ Name-> username indicates the public variable in the class. I do not need to explain the equal sign assignment.
Let's print it again:
$ Name-> show_username ();
Haha, I finally printed "pig's Deep Space. That's good. It's very convenient. You can modify the print value without using the form parameter ~~.
However, printing a name alone is too boring. Let's just say something you are Welcome to extend this class and create a function called Welcome:
Class my_class
{
Var $ username = "Deep Space ";
Function show_username ()
{
Echo $ this-> username;
}
Function Welcome ()
{
}
}
Well, what functions are implemented? Simply put, let's say "welcome" before the name.
Class my_class
{
Var $ username = "Deep Space ";
Function show_username ()
{
Echo $ this-> username;
}
Function Welcome ()
{
Echo "welcome ";
$ This-> show_username ();
}
}
The second time I saw $ this, right? Unlike the previous one, $ this-> show_username (); what should I do? Point to a function in the class. In fact, it calls the show_username function, and uses $ this to indicate that this function is in the class and is parallel to the Welcome function, rather than in other places (such as the Welcome function ).
The functions implemented by the Welcome function are very simple. Print the two words "Welcome" first, and then execute the show_username function to print the name.
Let's try this function:
$ Name-> Welcome ();
Now, I can print the words "Welcome to Deep Space.
But what should I do if I want to print "Welcome pig to deep space? I have served you. Let's give the public variable var $ username a value:
$ Name-> username = "pig deep space ";
Next, print the welcome speech:
$ Name-> Welcome ();
Hey, I finally printed "Welcome pig's Deep Space.
How is it? Have you understood the usage of the class? The advantage is that any function in the class can be called. As long as $ this is used to point out, the value of a public variable can be changed. this public variable can be used in functions in the class .......... The application is waiting for you to discover it.