Gym Class Heroes An introductory PHP class article

Source: Internet
Author: User
Tags php class
Just a glance at the first page to update the article about class (referring to the phpe of the http://www.phpe.net/articles/389.shtml), very good, suggest to see.
The exploration of the class ~ ~ I spent half a year to understand the role and realization of the class. There is not a single article that I can understand (I didn't touch anything oo before).
In my opinion, the class in PHP, the language used to express is informal language, and not sure whether it is correct.
Building a class is simple.
Class My_class {}
What exactly does a class do? A lot of people are what black box, I call it here as an independent whole. We only know the class name, and we don't know what's inside. So, how do you use this class?
First of all: to know if it defines a common variable--it's called "attributes" in the jargon.
Second: Be aware of what functions are defined in it--in the jargon, it is called "method."
I was confused by the jargon, so I just ignored it.
How does it work when you define a public variable in a class?
Very simple, let's expand the My_class class.
Class My_class
{
var $username;
}
Look at it very simple, we define a public variable, just use var+ space + ordinary variable name to make up. What's the use of it? Consider the function, if we want to access variables outside the function, do you want to first global? The same is true of the effect, which is to allow all functions in this class to have access to it, and it differs from one place in the function, that is, the outside of the class can also access the variable, and I then tell the external how to access it. There is also a difference, you can not use complex statements to assign values to this variable (concrete, such as to understand the class later to see the rules). Give it a default value
Class My_class
{
var $username = "Deep Space";
}
OK, define a common variable, then define a function (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. As simple as that, define a function for printing $username:
Class My_class
{
var $username = "Deep Space";
function Show_username ($username)
{
echo $username;
}
}
Here may be some people began to confuse, hehe, the most important thing is here, see clearly. There are now three $username. Which one is which AH ~ ~
Do you want to explain the line parameters of the function? The function is to print the value received by the row parameter, that is, if:
Show_username ("Pig head Deep Space");
Then it will print "pig head Deep Space", it is so simple.
How do I access this function? Certainly not as I said above directly show_username ("Pig head deep Empty"); , don't worry, class has a set of classes. As follows:
$Name = new My_class ();
This initializes the above My_class class, and assigns this object to the variable $Name, you can understand that this variable represents the whole class, hehe.
To use a function in a class:
$Name->show_username ("Pig head Deep Space");
Dizzy, why is it so complicated? And the arrows? Actually very image of. The class was already given the variable $Name, right? That is $Name represents the class, and then uses an arrow to point to the Show_username function in the class. It is that simple, that is to say, this function is in this class, not other functions--you understand to represent a difference, hehe.
Try it out, print out the four words "pig's deep Empty". Why do you think it's so complicated? Is it possible to do this with a function? I said, you certainly do not see the benefits of such a simple, we continue to expand.
There is one more question: "Public variables" just said, how useless? Why does this function not automatically receive the default value in this public variable var $username? That is, if I use:
$Name->show_username ($username);
What will be the result? The answer is no output. Because you did not $username a value for the parameter.
So how do you use this public variable? Let's change this class:
Class My_class
{
var $username = "Deep Space";
function Show_username ()
{
Echo $this->username;
}
}
Oh, no, it's all right. Also a $this-&gt, dizzy not, hehe. In fact, this is also one of the biggest convenience of the class.
$this: Access a public variable, or a function inside a class.
Access? So professional? In fact, it is to use $this->username instead of Var $username pull, $this to show that it is public. Something that can be accessed outside of the function.
Try:
$Name->show_username ();
See, finally print "Deep space" the two words, Wahaha.
I do not print "deep space" These two words, I want to print "Pig head deep Empty", how to do? Quite simply, we re-assign this public variable to pull. It's your service.
$Name->username = "Pig head deep Empty";
Does this make sense? $Name->username represents this common variable inside a class. The equal value assignment doesn't have to be explained.
Let's print it out again.
$Name->show_username ();
Haha, finally print "pig head deep Empty". Good, very convenient, without formal parameters can also be arbitrarily modified printing value Oh ~ ~.
But just printing a name is too boring, let's say a little welcome, to expand the class and create a function called Welcome:
Class My_class
{
var $username = "Deep Space";
function Show_username ()
{
Echo $this->username;
}
function Welcome ()
{
}
}
Well, what's the best function? Simply put it in front of the name "Welcome" two words good
Class My_class
{
var $username = "Deep Space";
function Show_username ()
{
Echo $this->username;
}
function Welcome ()
{
echo "Welcome";
$this->show_username ();
}
}
Did you see $this the second time? $this->show_username (); What do you do with it? In fact, it is called Show_username, which uses $this to represent the function in the class and parallel to the Welcome function, rather than elsewhere (such as the Welcome function).
The function of the Welcome function is very simple, first printing two words "welcome", then proceed to execute Show_username function, print the name.
Let's try this function:
$Name->welcome ();
See, print out the "Welcome deep Space" four words.
But I want to print "Welcome pig deep Empty", how to do? I have served you, we give the public variable var $username a value:
$Name->username = "Pig head deep Empty";
Then print the Welcome words:
$Name->welcome ();
Hey, finally print "Welcome pig deep Empty".
What do you think? Do you understand the usage of the class? The advantage is that you can invoke any function in the class, as long as it is indicated by the $this, you change the value of a public variable, and you can use that common variable in a function in a class. ......... More to go, its application waiting for you to find out.

The above describes the gym Class heroes an introductory PHP class article, including the gym Class heroes aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.