Some personal views on class in PHP

Source: Internet
Author: User
Tags define definition empty functions variables variable access
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;
}


Looking at the above is very simple, we have defined a common variable, only with var+ 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 ~ ~

function with the formal parameters, do not explain it? The function is to print the value received by the formal parameter, i.e. if:

Show_username ("Pig deep Empty");


Then it will print "pig head deep Empty", it is so simple.

How do I access this function? Definitely not the way I said it directly show_username ("Pig 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 the object to the variable $Name, you can understand that this variable represents the entire class, hehe.

To use a function in a class:

$Name->show_username ("Pig deep Empty");


Dizzy, why so complicated? and arrows? In fact, very image. The class has been given a 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's that simple, that is to say, the function is in this class, not the other function--you understand it as a difference, hehe.

Try Oh, print out "Pig deep empty" this four words. Why do you think it's so complicated? Is it possible to use a function? I said, you certainly do not see the benefits of such a simple, we continue to expand.

Another question is: "Public variables" just said how useless? Why does this function not automatically receive the default value in the Var $username of this public variable? Which is if I use:

$Name->show_username ($username);


What's going to happen? The answer is no output. Because you don't $username a value for the parameter.

So how do you use this public variable? Let's revise this class:

Class My_class
{
var $username = "Deep Space";

function Show_username ()
{
Echo $this->username;
}
}


Wow, no, this is not even a formal parameter? Also a $this-&gt, dizzy not, hehe. In fact, this is one of the greatest convenience of class.
$this function: Access to a public variable, or a function within a class.
Access? So professional? In fact, the $this->username instead of the Var $username pull, $this used to show that it is public, accessible, outside the function of things (such as other variables or functions).

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? It's simple, we're assigning this public variable to pull again. I've taken you.

$Name->username = "Pig deep Empty";


Does this make sense? $Name->username represents this public variable within the class. Equal value assignment I don't need to explain.

Let's print it out again:

$Name->show_username ();


Haha, finally print "pig head deep Empty". Good, very convenient bar, no formal parameters can be arbitrarily modify the printing value Oh ~ ~.

But simply printing a name is too boring, let's say something welcome to 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 is the realization of the function? Just make it easy, it's a "welcome" two word 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 ();
}
}


Did you see $this the second time? And the last time a little different, $this->show_username (); What's it for? Point to a function in a class, in fact it is called show_username this function, with $this to indicate that the function in the class and and Welcome function parallel, rather than in other places (such as the Welcome function).

The function of Welcome function is very simple, first print two words "welcome", then follow the Show_username function, print the name.

Let's try this function:

$Name->welcome ();


See, print out "Welcome deep space" This four words.

But I want to print "Welcome pig head Deep Empty", how to do? I took you, we give the public variable var $username a value bar:

$Name->username = "Pig deep Empty";


Next print the Welcome language:

$Name->welcome ();


Hey, finally print "Welcome pig deep Empty."

What do you think? Do you understand the use of the class? The advantage is that you can invoke any function in the class, as long as $this point out, you can change the value of a public variable, you can use the public variable in the function in the class. ......... Much has gone, and its application awaits you to find out.



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.