I just browsed the article about Class (referring to the article /articles/ of PHPE) updated on the homepage. It is very good, I suggest you take a look.
After exploring the class, it took me half a year to roughly understand the role and implementation of the class. The main reason is that there is no article that can make me understand (I haven't been exposed to anything OO before).
From my point of view, Class in PHP is used for expression. The languages used for expression are all informal languages, and it is not certain whether they are correct.
Creating a class is simple.
class my_class {}
What exactly does class do? Many people are black boxes, and I call it an independent whole here. We only know the class name, but not what is inside. So, how to use this class?
First of all: you need to know whether it defines a common variable - it is called "attribute" in professional terms.
Secondly: You need to know what function is defined in it - it is called "method" in professional terms.
I was confused by these professional terms, so I simply ignored it.
How to define public variables in a class and what does it do?
It's very simple, let's expand the my_class class
class my_class
{
var $username;
}
It looks very simple above. We defined a common variable, which is composed of var + space + ordinary variable names. What's the use of it? Consider the function. If we want to access variables outside the function, should we first global? The same is true for this. It wants all functions in this class to access it. One of the differences between it and the function is that the outside of the class can also access this variable. I will talk about how to access it outside. There is another difference: you cannot use complex statements to assign values to this variable (see the rules yourself after understanding the class). Give it a default value
class my_class
{
var $username = "Deep Space";
}
OK, define a common variable, and then define 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 ordinary definition function in form. Just simple, define a function that prints $username:
class my_class
{
var $username = "Deep Space";
function show_username($username)
{
echo $username;
}
}
Some people may be confused by this, haha, the most important thing is here, let’s see clearly. There are now three $usernames. Which one is which one~~
There is no need to explain the line parameters contained in the function, right? This function is to print the received value of the line parameter, that is, if:
show_username("Pig Head Deep Space");
Then it will print "Pig Head Deep Space", that's that simple.
How to access this function? It's definitely not as direct as I said above. show_username("Pig Head Deep Space");, don't worry, there are some types of classes. as follows:
$Name = new my_class();
In this way, the my_class class above is initialized and the object is assigned to the variable $Name. You can understand this way, and this variable represents the entire class, haha.
Use functions in the class:
$Name->show_username("pig head deep space");
Dizzy, why is it so complicated? Still need arrows? Actually very vivid. I have already given the class to the variable $Name, right? That is, $Name represents this class, and then use an arrow to point to the show_username function in the class. It's that simple, that is, this function is in this class, not other functions - you understand it as representing a difference, haha.
Try it, print out the four words "pig head deep space". Why do you think it is so complicated? Can't it be implemented using functions? I said, of course you can't see the benefits of such a simple one, so we will continue to expand.
There is another question: Why is the "public variable" mentioned just now useless? Why doesn't this function automatically receive the default value in this public variable var $username? That is, if I use:
$Name->show_username($username);
What will happen? The answer is that there is no output. Because you did not give the formal parameter $username a value.
So how do you use this public variable? Let's modify this class:
class my_class
{
var $username = "Deep Space";
function show_username()
{
echo $this->username;
}
}
Wow, no, this time, the face ginseng is gone? There is also an extra $this->, I'm dizzy, haha. In fact, this is also one of the biggest conveniences of the class.
The function of $this: access a public variable or a function in a class.
access? So professional? In fact, it is just to use $this->username instead of var$username, and $this is used to indicate that it is public. Accessible, external to the function.
Try it:
$Name->show_username();
I saw it, and finally printed the two words "Deep Space", Wahaha.
I don’t print the word “deep space”, I want to print “pig head deep space”, what should I do? It's very simple, we reassign the value to this public variable. I've convinced you.
$Name->username = "Pig Head Deep Space";
Can this understand the meaning? $Name->username represents the public variable in the class. I don't need to explain the equal sign assignment.
Let's print it again
$Name->show_username();
Haha, finally the "pig head deep space" is printed. Not bad, very convenient, you can modify the print value at will without using formal parameters~~.
But it is too boring to just print a name. Let’s say something welcome, expand 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 function is good to implement? Just make it simple, just implement the word "welcome" in front of the name.
class my_class
{
var $username = "Deep Space";
function show_username()
{
echo $this->username;
}
function Welcome()
{
echo “Welcome”;
$this->show_username();
}
}
I saw $this the second time, right? $this->show_username(); What is it for? In fact, it is called the show_username function, and $this is used to indicate that this function is in the class and is parallel to the Welcome function, rather than elsewhere (such as in the Welcome function).
The function of the Welcome function is very simple. First print two words "Welcome", and then execute the show_username function to print the name.
Let's try this function:
$Name->Welcome();
I saw it and printed out the four words "Welcome Deep Space".
But I want to print "Welcome Pig Head Deep Space", what should I do? I'm convinced you, let's give the public variable var $username a value:
$Name->username = "Pig Head Deep Space";
Next print the welcome message:
$Name->Welcome();
Hehe, finally print "Welcome Pig Head Deep Space".
How about it? Do you understand the usage of classes? The advantage is that it can call any function in the class. As long as it is pointed out with $this, the value of a common variable can be changed, and this common variable can be used in the functions in the class. ………There are too many, and its applications are waiting for you to discover.
After exploring the class, it took me half a year to roughly understand the role and implementation of the class. The main reason is that there is no article that can make me understand (I haven't been exposed to anything OO before).
From my point of view, Class in PHP is used for expression. The languages used for expression are all informal languages, and it is not certain whether they are correct.
Creating a class is simple.
class my_class {}
What exactly does class do? Many people are black boxes, and I call it an independent whole here. We only know the class name, but not what is inside. So, how to use this class?
First of all: you need to know whether it defines a common variable - it is called "attribute" in professional terms.
Secondly: You need to know what function is defined in it - it is called "method" in professional terms.
I was confused by these professional terms, so I simply ignored it.
How to define public variables in a class and what does it do?
It's very simple, let's expand the my_class class
class my_class
{
var $username;
}
It looks very simple above. We defined a common variable, which is composed of var + space + ordinary variable names. What's the use of it? Consider the function. If we want to access variables outside the function, should we first global? The same is true for this. It wants all functions in this class to access it. One of the differences between it and the function is that the outside of the class can also access this variable. I will talk about how to access it outside. There is another difference: you cannot use complex statements to assign values to this variable (see the rules yourself after understanding the class). Give it a default value
class my_class
{
var $username = "Deep Space";
}
OK, define a common variable, and then define 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 ordinary definition function in form. Just simple, define a function that prints $username:
class my_class
{
var $username = "Deep Space";
function show_username($username)
{
echo $username;
}
}
Some people may be confused by this, haha, the most important thing is here, let’s see clearly. There are now three $usernames. Which one is which one~~
There is no need to explain the line parameters contained in the function, right? This function is to print the received value of the line parameter, that is, if:
show_username("Pig Head Deep Space");
Then it will print "Pig Head Deep Space", that's that simple.
How to access this function? It's definitely not as direct as I said above. show_username("Pig Head Deep Space");, don't worry, there are some types of classes. as follows:
$Name = new my_class();
In this way, the my_class class above is initialized and the object is assigned to the variable $Name. You can understand this way, and this variable represents the entire class, haha.
Use functions in the class:
$Name->show_username("pig head deep space");
Dizzy, why is it so complicated? Still need arrows? Actually very vivid. I have already given the class to the variable $Name, right? That is, $Name represents this class, and then use an arrow to point to the show_username function in the class. It's that simple, that is, this function is in this class, not other functions - you understand it as representing a difference, haha.
Try it, print out the four words "pig head deep space". Why do you think it is so complicated? Can't it be implemented using functions? I said, of course you can't see the benefits of such a simple one, so we will continue to expand.
There is another question: Why is the "public variable" mentioned just now useless? Why doesn't this function automatically receive the default value in this public variable var $username? That is, if I use:
$Name->show_username($username);
What will happen? The answer is that there is no output. Because you did not give the formal parameter $username a value.
So how do you use this public variable? Let's modify this class:
class my_class
{
var $username = "Deep Space";
function show_username()
{
echo $this->username;
}
}
Wow, no, this time, the face ginseng is gone? There is also an extra $this->, I'm dizzy, haha. In fact, this is also one of the biggest conveniences of the class.
The function of $this: access a public variable or a function in a class.
access? So professional? In fact, it is just to use $this->username instead of var$username, and $this is used to indicate that it is public. Accessible, external to the function.
Try it:
$Name->show_username();
I saw it, and finally printed the two words "Deep Space", Wahaha.
I don’t print the word “deep space”, I want to print “pig head deep space”, what should I do? It's very simple, we reassign the value to this public variable. I've convinced you.
$Name->username = "Pig Head Deep Space";
Can this understand the meaning? $Name->username represents the public variable in the class. I don't need to explain the equal sign assignment.
Let's print it again
$Name->show_username();
Haha, finally the "pig head deep space" is printed. Not bad, very convenient, you can modify the print value at will without using formal parameters~~.
But it is too boring to just print a name. Let’s say something welcome, expand 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 function is good to implement? Just make it simple, just implement the word "welcome" in front of the name.
class my_class
{
var $username = "Deep Space";
function show_username()
{
echo $this->username;
}
function Welcome()
{
echo “Welcome”;
$this->show_username();
}
}
I saw $this the second time, right? $this->show_username(); What is it for? In fact, it is called the show_username function, and $this is used to indicate that this function is in the class and is parallel to the Welcome function, rather than elsewhere (such as in the Welcome function).
The function of the Welcome function is very simple. First print two words "Welcome", and then execute the show_username function to print the name.
Let's try this function:
$Name->Welcome();
I saw it and printed out the four words "Welcome Deep Space".
But I want to print "Welcome Pig Head Deep Space", what should I do? I'm convinced you, let's give the public variable var $username a value:
$Name->username = "Pig Head Deep Space";
Next print the welcome message:
$Name->Welcome();
Hehe, finally print "Welcome Pig Head Deep Space".
How about it? Do you understand the usage of classes? The advantage is that it can call any function in the class. As long as it is pointed out with $this, the value of a common variable can be changed, and this common variable can be used in the functions in the class. ………There are too many, and its applications are waiting for you to discover.