PHP Object-oriented class_php tutorial

Source: Internet
Author: User
The following is the class in PHP, the language used for expression is informal language.

Building a class is simple:
Class My_class {}

What exactly does a class do? A lot of people say it's a black box, and here I call it an independent whole. We only know the class name, and we don't know what's inside. So, how do you use this class?
First of all: to know if it defines a common variable--it's called "attributes" in the jargon.
Second: Be aware of what functions are defined in it--in the jargon, it is called "method."
I was confused by the jargon, so I just ignored it.

How does it work when you define a public variable in a class?

Quite simply, let's expand the My_class class:

Class My_class
{
var $username;
}

Look at it very simple, we define a public variable, just with the Var space common variable name. What's the use of it? Consider the function, if we want to access variables outside the function, do you want to first global? This is the way to achieve the effect, 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 at any time, I then talk about how the external access to it. There is also a difference, you can not use complex statements to assign values to this variable (concrete, such as to understand the class later to see the rules).

Give it a default value:

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

OK, define a common variable, then define a function (the so-called "method"):

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

function Show_username ()
{
}
}

This definition function is no different from the normal definition function form. As simple as that, define a function for printing $username:

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

function Show_username ($username)
{
echo $username;
}
}

Here may be some people began to confuse, hehe, the most important thing is here, see clearly. There are now three $username. Which one is which AH ~ ~

Do you need to explain the formal parameters of the function? The 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 head Deep Space", it is so simple.

How do I access this function? Certainly not as I said above directly show_username ("Pig head 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 that this variable represents the whole class, hehe.

To use a function in a class:

$Name->show_username ("Pig head Deep Space");

Dizzy, why is it so complicated? And the arrows? Actually very image of. The class was already given the 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 is that simple, that is to say, this function is in this class, not other functions--you understand to represent a difference, hehe.

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

There is also a question: "Public variables" just said, why is it useless? Why does this function not automatically receive the default value in this public variable var $username? That is, if I use:

$Name->show_username ($username);

What will be the result? The answer is no output. Because you did not $username a value for the parameter.

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

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

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

Oh, no, it's not even a formal parameter. Also a $this-&gt, dizzy not, hehe. In fact, this is also one of the biggest convenience of the class.
$this: Access a public variable, or a function inside a class.
Access? So professional? In fact, it is to use $this->username instead of Var $username pull, $this to show that it is public, accessible, external functions (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? Quite simply, we re-assign this public variable to pull. It's your service.

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

Does this make sense? $Name->username represents this common variable inside a class. The equal value assignment doesn't have to be explained.

Let's print it again to see:

$Name->show_username ();

Haha, finally print "pig head deep Empty". Good, very convenient, without formal parameters can also be arbitrarily modified printing value Oh ~ ~.

But just printing a name is too boring, let's say a little welcome, to expand the class and create a function called Welcome:

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

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

function Welcome ()
{
}
}

Well, what's the best function? Simply put it in front of the name "Welcome" two words good

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? A little different from last time, $this->show_username (); What do you do with it? Point to a function in the class, in fact it is called show_username this function, with $this to represent the function in the class and parallel to the Welcome function, rather than in other places (such as the Welcome function).

The function of the Welcome function is very simple, first printing two words "welcome", then proceed to execute Show_username function, print the name.

Let's try this function:

$Name->welcome ();

See, print out the "Welcome deep Space" four words.

But I want to print "Welcome pig deep Empty", how to do? I have served you, we give the public variable var $username a value:

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

Then print the Welcome words:

$Name->welcome ();

Hey, finally print "Welcome pig deep Empty".

What do you think? Do you understand the usage of the class? The advantage is that you can invoke any function in the class, as long as it is indicated by the $this, you change the value of a public variable, and you can use that common variable in a function in a class. ......... More to go, its application waiting for you to find out.

http://www.bkjia.com/PHPjc/486214.html www.bkjia.com true http://www.bkjia.com/PHPjc/486214.html techarticle The following is the class in PHP, the language used for expression is informal language. Building a class is simple: What exactly does class My_class {} do? Many people say what a black box ...

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