An entry-level Class document _ PHP Tutorial

Source: Internet
Author: User
An entry-level Class article. Very good. we recommend that you check it out. Exploration of classes ~~ I just browsed the Class-related article updated on the homepage (referring to the PHPE article ). http://www.phpe.net/articles/389.shtml ), 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. PHP code: define class my_class {}-------------------------------------------------------------------------------- what is the class doing? 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 PHP code of my_class class: Keep class my_class {var $ username;} else. let's look at the above very simple. we have defined a public variable, it is composed of var + space + common variable name. 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 PHP code: define class my_class {var $ username = "";} define OK, defines a public variable, next, define a function (the so-called "method "). PHP code: define class my_class {var $ username = ""; function show_username () {}} this definition function is no different from a common definition function. Just put, define a function to print $ username: PHP code: using class my_class {var $ username = ""; function show_username ($ username) {echo $ username ;}} -------------------------------------------------------------------------------- some people may be confused here. the most important thing is here. I have read it clearly. There are now three $ usernames. Which is it ~~ Do not explain the row parameters included in the function? This function is used to print the value received by the row parameter, that is, if the PHP code: export show_username ("pig deep space"); then it prints "pig deep space ", that's 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. PHP code: Require $ Name = new my_class (); then initialize the above my_class class and assign this object to the variable $ Name, as you can understand, this variable represents the entire class. Use the function in the class: PHP code: ---------------------------------------------------------------------------- $ Name-> show_username ("pig "); 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: PHP code: ------------------------------------------------------------------------------ $ Name-> show_username ($ username); what will happen to the supervisor? 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: PHP code: using class my_class {var $ username = ""; function show_username () {echo $ this-> username ;}} wow, no, isn't there any face parameters? 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. Let's try: PHP code: Pipeline $ Name-> show_username (); then, let's see it and finally print the word "Deep Space", 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. PHP code: ---------------------------------------------------------------------------------- $ Name-> username = "pig deep space"; can you understand this? $ Name-> username indicates the public variable in the class. I do not need to explain the equal sign assignment. Let's print the PHP code: Emerge $ Name-> show_username (); ------------------------------------------------------------------------ haha, and finally print "pig 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: PHP code: using class my_class {var $ username = ""; function show_username () {echo $ this-> username;} function Welcome () {}} Yun, what functions are implemented? Simply put, let's say "welcome" in front of the name. PHP code: Keep class my_class {var $ username = ""; function show_username () {echo $ this-> username;} function Welcome () {echo "Welcome"; $ this-> show_username () ;}} second time I saw $ this? $ 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: PHP code: Welcome $ Name-> Welcome (); then we can see it and print out the four words "Welcome to deep space. But what should I do if I want to print "welcome pig to deep space? Now that I have served you, let's give the public variable var $ username a value: PHP code: success $ Name-> username = "pig deep space"; continue and print the welcome speech: PHP code: Welcome $ 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 ~~...

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.