Joke Collection Website - Talk about mood - Some personal views on classes in PHP

Some personal views on classes in PHP

Author: Deep Space Source: Beyond PHP From my point of view, the language used to express classes in PHP is informal, and I am not sure whether it is correct. Creating a class is simple: class my_class {}

What exactly does a class do? Many people say it is a black box, and I call it an independent whole here. We only know the class name, but we don't know what's in it. So how do you use this class?

The first thing to know is whether there is a public variable defined in it-in technical terms, it is called "attribute".

Second: know what functions are defined in it-in technical terms, it is called "method".

I was confused by these technical terms and just ignored them. How to define a public variable in a class, and what does it do? Very simple, let's expand my _class class: class my _class.

{

var $ username

}

Look at the top. It's simple. We define a public variable, which only consists of var+ space+public variable name. What's the use? Think about function. If we want to access variables outside the function, do we have to globalize them first? The same is true for this purpose. It hopes that all functions in this class can access it. One difference between it and functions is that the variable can be accessed and controlled at any time outside the class. I will discuss how to access it from the outside later. Another difference is that you can't assign a value to this variable with complicated statements (specifically, you can read the rules yourself after you understand the class). Give it a default value: class my_class.

{

Var $username = "deep space";

}

Ok, define a public variable, and then define a function (so-called "method"): class my_class.

{

Var $username = "deep space"; Function show _ user name ()

{

}

}

This definition function is no different from the ordinary definition function in form. Just define a function that prints $username: class my_class.

{

Var $username = "deep space"; Function show _ username ($username)

{

echo $ username

}

}

Maybe some people are beginning to get confused here, hehe, the most important thing is here, see clearly. There are now three $ usernames. Which is which? The formal parameters of the function need not be explained, right? The function of this function is to print the value received by the formal parameter, that is, if: show_username ("pig head deep space");

Then it will print "pig's head deep space", as simple as that. How to access this function? It is definitely not the direct show_username I mentioned above ("pig head deep space"); Come on, don't worry, there is a class. As follows: $ name = newmy _ class ();

This will initialize the class of my_class above and assign the object to the variable $Name. You can understand that this variable represents the whole class, hehe. Use the function in the class: $ Name-& gt;; Show_username ("pig's head deep space");

Gosh, why is it so complicated? More arrows? It's actually very vivid. You have assigned this class to the variable $Name, right? That is, $Name stands for this class, and then point to the function show_username in the class with an arrow. It's as simple as that, that is, this function belongs to this class, not other functions-just understand it as a difference, hehe. Try to print out the words "pig's head deep space" Why do you think it is so complicated? Can't it be realized by a function? I said, of course you can't see such a simple benefit. Let's continue to expand. Another question is: Why is the "public variable" mentioned just now useless? Why doesn't this function 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 assign a value to the parameter $username. So how do you use this public variable? Let's modify this class: class my_class.

{

Var $username = "deep space"; Function show _ user name ()

{

echo $ this-& gt; User name;

}

}

Wow, really, there are no formal parameters this time? There is also a $ this-& gt;; , dizzy. No, hehe. In fact, this is also one of the biggest conveniences in class.

The function of $this is to access public variables or functions in a class.

Visit? So professional? Actually, it' s $ this->; Username instead of var $username, $ This is used to indicate that it is public, accessible and something other than a function (such as other variables or functions). Give it a try: $ Name-& gt;; Show _ username ();

See, I finally printed the word "deep space", Wahaha. I don't print the word "deep space", but I want to print "pig's head deep space". What should I do? It's simple. Let's redistribute this common variable. I'm impressed. $ Name-& gt; Username = "Pig's head deep space";

Can you understand this? $ Name-& gt; Username represents this public variable in the class. I don't need to explain the assignment of the equal sign. Let' s print it again: $ Name-& gt;; Show _ username ();

Haha, I finally printed "Deep Space of Pig's Head". Not bad, is it convenient? You can modify the printed value at will without formal parameters. But just printing a name is boring. Let's talk about some Welcome extensions to this class and create a function called Welcome: class my_class.

{

Var $username = "deep space"; Function show _ user name ()

{

echo $ this-& gt; User name;

} Welcome function ()

{

}

}

Well, what function is good? To put it simply, just have the word "welcome" in front of your name. My class.

{

Var $username = "deep space"; Function show _ user name ()

{

echo $ this-& gt; User name;

} Welcome function ()

{

Echoing "welcome";

$ this-& gt; Show _ username ();

}

}

Have you seen $this for the second time? A little different from last time, $ this-& gt;; Show _ username (); What is this for? Pointing to a function in a class is actually calling the function show_username, and using $this means that the function is in the class, parallel to the Welcome function, but not elsewhere (such as the Welcome function). The function realized by the welcome function is very simple. Print the word "welcome" first, and then execute the show_username function to print the name. Try this function: $ Name-& gt;; welcome();

Look, the words "Welcome to Deep Space" are printed. But I want to print "Welcome to the Deep Space of Pig's Head". What should I do? I'm impressed with you. Let's give the variable var $username a value: $ name->; Username = "Pig's head deep space";

Next, print the welcome message: $ Name-& gt;; welcome();

Hehe, I finally printed out "Welcome to the Deep Space of Pig's Head". How's it going? Do you know the usage of classes? The advantage is that you can call any function in the class. As long as you use $this to point out, you can change the value of a public variable, which can be used in functions in a class. There are still many applications waiting for you to discover.