PHP class entry classic article

xiaoxiao2021-03-06  64

Some personal opinions about Class in PHP ------------------------------------------------------ ---------------------------------------- author: deep space source: beyond just in PHP Ontario browsing a first page update article about Class's articles, very good, suggestion see: http://www.phpe.net/articles/389.shtml

The exploration of the class ~~ 俺 uses half a year to understand the role and implementation of the class. Mainly there is no article that makes me understand (there have been no more OO before it.). In my point of view, the class in PHP is said, and the language used to express is a non-formal language, and it cannot be determined whether it is correct.

Establish a class is very simple:

Class my_class {}

What is the class? Many people say what black box, I call it as a separate whole. We only know the name of the class, and don't know what is in it. So, such as

What is this class? First of all: To know if it defines a public variable - the professional term is called "attribute." Second: Do you know what function is defined - it is called "method" in the professional term. I have been confused by these major terms, so I just ignore it.

How do I have a common variable in the class?

Very simple, let's expand the MY_CLASS class:

Class my_class {var $ usrname;}

It is very simple to see, we define a public variable, just consist of Var space ordinary variable name. What is it used? Consider the function, if we want to access functions

Is there a variable outside? Is it going to Global? This is the same, it is true that it wants to let all functions in this class can access it, and it distinguishes a place to function.

It is also an externally accessible and controlling this variable at any time, and I will then talk about how the outside is accessible. There is also a difference that cannot be assigned to this variable with complex statements (specific equation

Solve yourself after you look at the rules after the class).

Give it a default value:

Class my_class {var $ usrname = "deep";

OK, define a public variable, next to define a function (that is, the so-called "method"):

Class my_class {var $ usrname = "deep";

Function show_username () {}}

This definition function has no difference in the form of a normal definition function. Just, define a function of printing $ usrname:

Class my_class {var $ usrname = "deep";

Function show_username ($ usrname) {echo $ usrname;}}

It may be fascinating here, huh, huh, the most critical is here, see it clear. There are now three $ usrname. Which one is it? ~~

The shape of the function belt, don't explain it? This function function is to print the value of the confessed value, that is, if:

Show_username ("deep space");

Then it will print "deep space", just as simple.

How to access this function? It is definitely not that I have so-saying it directly show_username ("Sterling of the pig");, don't worry, there is a set of classes. As follows: $ name = new my_class ();

This initializes the type of my_class above, and assigns this object to a variable $ name, you can understand this, this variable represents the entire class, huh, huh.

Use the functions in the class:

$ Name-> show_username ("deep space");

Dizziness, why is this complicated? Also arrow? In fact, it is very imageable. Originally gave the class to the variable $ name? That is, $ name represents this class, then use an arrow

Head points to show_username this function in the class. It is such a simple, that is, this function is in this class, not other functions - you understand that

Let's, huh, huh.

Try it, print the four words of "deep space". Why are you so complicated? Is it possible to use a function? I said, you certainly don't see good.

We continue to expand.

There is also a question: How did the "public variable" just said? Why does this function will automatically receive the default in this public variable var $ username?

value? That is, if I use:

$ Name-> show_username ($ usrname);

What do you have? The answer is not any output. Because you don't give the parameter $ username a value.

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

Class my_class {var $ usrname = "deep";

Function show_username () {echo $ this-> username;}}

Wow, not, don't you have? There are more $ this->, dizzy, huh, huh. In fact, this is also a biggest convenience of the class. $ This role: Access a public variable, or a function in the class. Visit? Is this professional? In fact, use $ this-> username instead of Var $ username, $ this is used to explain it is public, can be accessed, external

Things (such as other variables or functions).

Try it:

$ Name-> show_username ();

I saw it, I finally printed two words "deep and empty", Wahaha.

I don't print "deep space", I want to print "deep space of pig", what should I do? Very simple, we re-value this public variable. I took you.

$ Name-> username = "The pig head is deep";

Can this understand what it means? $ Name-> username is a common variable in the class. I don't need to explain it.

Let's print again:

$ Name-> show_username ();

Haha, I finally printed "the pig's head is deep". Yes, it is very convenient, you can also modify the printed value without the shape.

But only one name is also too interesting, let's say something, let's expand this class, create a function called Welcome:

Class my_class {var $ usrname = "deep";

Function show_username () {echo $ this-> username;} function welcome () {}}

Well, what function is good? Simply click, you will be "welcome" in front of the name.

Class my_class {var $ usrname = "deep";

Function show_username () {Echo $ this-> username;}

Function welcome () {echo "Welcome"; $ this-> show_username ();}}

I saw $ this second time? And the last time, $ this-> show_username (); what is it used? Point to a function in the class, actually it is called

SHOW_USERNAME This function is used to represent this function in the class and and the Welcome function is parallel, not in other places (such as Welcome function).

The function of the Welcome function is very simple. First print two words "welcome", then take the show_username function, print the name.

Try this function:

$ Name-> Welcome ();

I saw it, and I printed "Welcome Dead".

But I want to print "Welcome to the pig's head", what should I do? I have served you, we give the public variable Var $ username a value:

$ Name-> username = "The pig head is deep";

Next print, welcome language:

$ Name-> Welcome ();

Hey, finally print "Welcome to the deep air".

how about it? Understand the usage of the class? The advantage is that it is possible to call any of the functions in the class. As long as you point out with $ this, you can change the value of a common variable, you can write in the class

This public variable is used in the number. ......... I have went, and it's the app to wait for you.

See the discussion area: http://club.phpe.net/index.php? ACT = ST & F = 2 & t = 4688

Note: This article is the original article, copyright belongs to the author and exceeding the PHP website, and no commercial reprint is prohibited without this site. Non-profit websites and personal websites reproduced please indicate the source, thank you

Xie cooperation! (Responsible Editor: Beyond PHP) [Recommended to a friend] [Show Print version] Update: 2004-04-06 Views: 2260

转载请注明原文地址:https://www.9cbs.com/read-112910.html

New Post(0)