Some personal views on Class in PHP

Source: Internet
Author: User
Tags empty variables
Just a cursory glance at the first page of the updated article about class, very good, suggest to see:


http://www.phpe.net/articles/389.shtml





to the kind of groping ~ ~ I spent half a year to probably understand the role and implementation of the class. 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 not sure whether it is correct or not.





building a class is simple:





class My_class {}







What exactly is the
class doing? 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 in the jargon, which is called "attribute".


Second: You need to know what functions it defines--in the jargon, it's called "method."


I was confused by these jargon, so I simply ignored it.




What does it do to define a public variable in the
class?





is simple, 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, defines a common 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 form of a defined 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? This function is the value that the print parameter receives, i.e. if:





show_username ("Pig deep Empty");








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




How does
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 this object to the variable $Name, you can understand this, this variable represents the whole class, hehe.





using Functions 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.





also has the question: "The public variable" just said has not been useful at all? 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 will happen to
? 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;


}


}








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

The role of
$this: Accessing a public variable, or a function within a class.


visit? 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 ();








saw it, and finally printed the word "Deep space", 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.





We'll 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 a few words of welcome, to expand this class, to create a function called Welcome:





class My_class


{


var $username = "Deep Space";





function Show_username ()


{


Echo $this->username;


}





function Welcome ()


{


}


}








, what is the best function to achieve? Be simple, just realize in front of the name has "welcome" two words good





class My_class


{


var $username = "Deep Space";





function Show_username ()


{


Echo $this->username;


}





function Welcome ()


{


echo "Welcome";


$this->show_username ();


}


}








See $this for 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 execute 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 Welcome:





$Name->welcome ();








Hey, finally print "Welcome pig deep Empty."




What about
? 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.