Dynamic Web technology Some tips for using classes in PHP

Source: Internet
Author: User

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

Building a class is simple:

Class My_class {}

What exactly does a class do? 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--the jargon is called "attributes."
Second: You need to know what functions it defines--in the jargon, it's called "method."
I was confused by the jargon, so I just ignored it.

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

Very simply, let's expand the My_class class:

Class My_class
{
var $username;
}

Look at the above is very simple, we defined a common variable, only with the Var white 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, define a common variable, and then define 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 the definition 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 ~ ~

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.