We recommend an entry-level Class article _ PHP Tutorial.

Source: Internet
Author: User
We recommend an entry-level Class article. Very good. we recommend that you check it out. Exploration of classes ~~ I just browsed the article about Class updated on the home page in the scope (refers to the http://www.phpe.net/articles/389.shtml of PHPE), very good, it is recommended to look.
Exploration of classes ~~ It took me half a year to understand the role and implementation of the class. There is not an article that can help me understand (I have never touched any OO stuff before ).
In my opinion, the Class used in PHP is an informal language and cannot be determined whether it is correct.
It is easy to create a class.
Class my_class {}
What is the class? Many people use black boxes. here I refer to them as 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 is used to allow all functions in this class to access it, and it is different from the function, and the variable can be accessed outside the class, 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 not explain the row parameters included in the function? This function prints the value received by the row 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 "public variables" that I just mentioned that are 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, no, isn't it? 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, $ this indicates that it is public. Something that can be accessed outside the function.
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? $ This-> show_username (); what should I do? 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.

Explain (refers to the http://www.phpe.net/articles/389.shtml of PHPE), very good, it is recommended to look. Exploration of classes ~~ I...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.