The kind of groping ~ ~ I spent half a year to understand the role of the class and implementation. The main thing is that I don't have an article that I can understand (I haven't touched anything oo before).
In my opinion, the class in PHP, the language used to express is both an informal language, and it is not certain that it is correct.
Building a class is simple:
Class My_class {}
What exactly does a class do? A lot of people say is what the black box, I call it here as an independent whole. We only know the class name, and we don't know what's in it. So, how do you use this class?
First: You know whether it defines a public variable--the jargon is called "attributes."
Second: You need to know what functions it defines--in the jargon, it's called "method."
I was confused by the jargon, so I just ignored it.
What does it do to define a public variable in a class?
Very simply, let's expand the My_class class:
Class My_class
{
var $username;
}
Look at the above is very simple, we defined a common variable, only with the Var white space ordinary variable name composition. What's the use of it? Consider the function, if we want to access the variables outside the function, is it a global first? This is also the effect of the implementation, it is to let all the functions in this class can access it, and it is different from the function of a place, is the outside of the class can also access and control the variable, I later talk about how to access it outside. There is also a difference, you can not use complex statements to assign the variable value (specific, such as understanding the class to see the rules themselves).
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 normal form of the definition function. Simply, define a function that prints $username:
Class My_class
{
var $username = "Deep Space";
function Show_username ($username)
{
echo $username;
}
}
Here may some people began to be confused, hehe, the most crucial is here, see clearly. There are now three $username. which is which AH ~ ~