Tutorials to .com

Tutorials to .com » Php » Others » Class on the PHP in the views of a few individuals

Class on the PHP in the views of a few individuals

Print View , by: iSee ,Total views: 3 ,Word Count: 1581 ,Date: Sun, 19 Apr 2009 Time: 8:35 PM

Just a bit rough browser page to update an article on the Class of the articles, very good, it is recommended to see:
http://www.phpe.net/articles/389.shtml

On the kind of feeling ~ ~ I spent only about six months to understand the role of class and realized. The main one is not allow me to understand the article (did not come into contact with before any of the things OO).
In my point of view that the php in the Class, for the expression of the language is the language of informal, but also not sure is correct.

The establishment of a category is very simple:

class my_class ()


Category in the end do? Many people say that what the black box, I would like to call it an independent entity. We only know the class name, do not know what's inside. So, how does the use of this category?
First of all: to know whether it is inside the definition of a public variable - call it a professional terminology "Properties."
Second: To know what the definition of its function inside - the professional jargon calls it "method."
I have been confused these terms, so simply ignore it.

How to define the category of public variables, it has what effect?

Very simple, we have to expand my_class categories:

class my_class
(
var $ username;
)


See above is very simple, we define a public variable, but with a space + var + variable names constitute ordinary. How does it use? Consider the function, if we want to access variables outside the function is not the first global word? Would like to achieve the effect of this is the case, it is to make the class of all functions can access it, and its function from a place outside the class can also access and control the variables, I then repeat the external how to access it. There is also a difference between the complexity of the statement can not be used to assign the variables (such as a specific understanding of the category to see beyond their own rules).

To give it a default value:

class my_class
(
var $ username = "deep space";
)


OK, the definition of a public variable, and the following definition of a function (that is, the so-called "method"):

class my_class
(
var $ username = "deep space";

function show_username ()
(
)
)


The general definition of function with no formal definition of the difference between the function. Simple, $ username define a print function:

class my_class
(
var $ username = "deep space";

function show_username ($ username)
(
echo $ username;
)
)


Some people here may be the beginning of a confused, huh, huh, is the most critical here, to see the picture clearer. Now there are three of the $ username. Which is which in the end ah ~ ~

Function brought about by the shape parameter, do not explain it? This is the print function function parameter value received, that is, if:

show_username ( "pig deep space");


It will print "deep space pig", as simple as that.

How to access this function? Certainly not, as I said above, direct show_username ( "pig deep space"); and do not worry, there is a set of categories. As follows:

$ Name = new my_class ();


This will initialize the above class of my_class and assign the object variable $ Name, you can interpret it this way, the variables on behalf of the entire category of, huh, huh.

The use of the function:

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


Halo, why so complicated? Would also like the arrow? In fact, the very image. Already the type to the variable $ Name of the is not it? $ Name is the representative of the category, and then an arrow pointing show_username this type of function. Is that simple, that is to say, this function is in this category, rather than a function of the other - you will understand that a distinction for you, huh, huh.

Give it a try Oh, print out "pig deep space," the words. Why do you say so complicated? Function not to achieve it? I said, so simple you do not see the benefits of course, we continue to expand.

Another question is: just said "public variable" how useful that are not? Why this function does not automatically receive this public variables var $ username in the default value? That is, if I use:

$ Name-> show_username ($ username);


What will be the result? The answer is there is no output. Because you do not have to type a value of $ username parameter.

Then how to use the public variables? We have to modify this category:

class my_class
(
var $ username = "deep space";

function show_username ()
(
echo $ this-> username;
)
)


, is not it, this parameter is not even a? More than a $ this->, a halo is not, huh, huh. In fact, this is a category of the greatest convenience.
$ this role: access to a public variable inside a function or category.
Visit? Such a professional? In fact, is to use $ this-> username instead of just var $ username pull, $ this used to describe it is a public, accessible, function of external things (for example, other variables or functions).

Give it a try:

$ Name-> show_username ();


See it, and finally print "Deep Space" This is the word, and Wahaha.

I do not print, "Deep Space" This is the word, I would like to print "deep space pig", how should I do? Very simple, we have to re-assign the variable pull public. Served you.

$ Name-> username = "pig deep space";


To understand what this means? $ Name-> username that is inside the category of public variables. I do not have to equate to explain the assignment.

Let us now look at the print:

$ Name-> show_username ();


Haha, finally print "pig deep space," the. Yes it is very easy now, do not shape parameters can be modified to print the value of arbitrary Oh ~ ~.

But printing alone is too boring a name, we say a word of welcome to you, to expand this category to create a function called Welcome:

class my_class
(
var $ username = "deep space";

function show_username ()
(
echo $ this-> username;
)

function Welcome ()
(
)
)


TU, what features to achieve good? It simply points on the front to achieve in the name of "welcome" the good word

class my_class
(
var $ username = "deep space";

function show_username ()
(
echo $ this-> username;
)

function Welcome ()
(
echo "welcome";
$ this-> show_username ();
)
)


$ This the second time to see now? And the last little bit different, $ this-> show_username (); what use? Category point to a function, in fact, it is show_username call this function, use $ this to show that this function in the class and the Welcome Function and parallel rather than in other places (such as the Welcome function).

Welcome to achieve the function of function is very simple, first of all, print the word "Welcome", and then followed the implementation of show_username function, print the name.

Try this function to Bar:

$ Name-> Welcome ();


See it, print out the "welcome to deep space," the words.

But I want to print "Welcome to deep space pig", how should I do? I served you, we give the public variables var $ username it a value:

$ Name-> username = "pig deep space";


Welcome to the next print language:

$ Name-> Welcome ();


, finally print "Welcome pig deep space," the.

How to? Understand the type of usage of it? The benefits of being able to call any type of function, as long as the use of $ this pointed out to, you can change the value of a public variable, you can function in the class to use the public variables. ... ... ... More to go, and its application to wait for you to discover the.


php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.