PHP Class (Class) Getting Started Tutorial 1th/2 page _php Basics

Source: Internet
Author: User
Tags php class
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.
Copy Code code as follows:

<?php
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.
Copy Code code as follows:

<?php
Class My_class
{
var $username;
}
?>

Looking at the above is very simple, we have defined a common variable, only with var+ 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 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
Copy Code code as follows:

<?php
Class My_class
{
var $username = "Deep Space";
}
?>

OK, define a common variable, and then define a function (that is, the so-called "method").
Copy Code code as follows:

<?php
Class My_class
{
var $username = "Deep Space";

function Show_username ()
{
Function: Method-> the effect to be implemented
}
}
?>

This definition function is no different from the normal form of the definition function. Simply, define a function that prints $username:
Copy Code code as follows:

<?php

Class My_class
{
var $username = "Deep Space";
function Show_username ($username)
{
Print
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 ~ ~
The line parameter that the function takes, don't you want to explain? The function is to print the values received by the row parameters, that is, if:
Copy Code code as follows:

<?php
Show_username ("Pig deep Empty");
?>

Then it will print "pig head deep Empty", it is so simple.
How do I access this function? Definitely not the way I said it directly show_username ("Pig deep Empty"); , don't worry, class has a set of classes. As follows:
Copy Code code as follows:

<?php
$Name =new My_class ();
?>

This initializes the above My_class class, and assigns the object to the variable $Name, you can understand that this variable represents the entire class, hehe.
To use a function in a class:
Copy Code code as follows:

<?php
$Name->show_username ("Pig deep Empty");
?>

Dizzy, why so complicated? and arrows? In fact, very image. The class has been given a 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's that simple, that is to say, the function is in this class, not the other function--you understand it as a difference, hehe.
Try Oh, print out "Pig deep empty" this four words. Why do you think it's so complicated? Is it possible to use a function? I said, you certainly do not see the benefits of such a simple, we continue to expand.
There is another question: what is the use of "public variables" that you have just said? Why does this function not automatically receive the default value in the Var $username of this public variable? Which is if I use:
Copy Code code as follows:

<?php
$Name->show_username ($username);
?>

What's going to happen? The answer is no output. Because you don't $username a value for the parameter. So how do you use this public variable?
Let's revise this class:
Copy Code code as follows:

<?php
Class My_class
{
var $username = "Deep Space";
function Show_username ()
{
Echo $this->username;
}
}
?>

Oh, no, no, this time there's no face. Also a $this-&gt, dizzy not, hehe. In fact, this is one of the greatest convenience of class.
$this function: Access to a public variable, or a function within a class.
Access? So professional? In fact, $this->username instead of the Var $username pull, $this to show that it is public. Something that can be accessed outside of the function.
Try:
Copy Code code as follows:

<?php
$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? It's simple, we're assigning this public variable to pull again. I've taken you.
Copy Code code as follows:

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

Does this make sense? $Name->username represents this public variable within the class. Equal value assignment I don't need to explain.
Let's print it out again.
Copy Code code as follows:

<?php
$Name->show_username ();
?>

Haha, finally print "pig head deep Empty". Good, very convenient bar, no formal parameters can be arbitrarily modify the printing value Oh ~ ~.
But simply printing a name is too boring, let's say something welcome to expand this class and create a function called Welcome:
Copy Code code as follows:

<?php
Class My_class
{
var $username = "Deep Space";
function Show_username ()
{
Echo $this->username;
}
function Welcome ()
{
}
}
?>

Well, what is the realization of the function? Just make it easy, it's a "welcome" two word in front of the name.
Copy Code code as follows:

<?php
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? $this->show_username (); What's it for? In fact, it's called Show_username, which uses $this to denote that the function is in the class and is parallel to the Welcome function, not elsewhere (such as the Welcome function).
The function of Welcome function is very simple, first print two words "welcome", then follow the Show_username function, print the name.
Let's try this function:
Copy Code code as follows:

<?php
$Name->welcome ();
?>

See, print out "Welcome deep space" This four words.
But I want to print "Welcome pig head Deep Empty", how to do? I took you, we give the public variable var $username a value bar:
Copy Code code as follows:

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

Next print the Welcome language:
Copy Code code as follows:

<?php
$Name->welcome ();
?>

Hey, finally print "Welcome pig deep Empty."
What do you think? Do you understand the use of the class? The advantage is that you can invoke any function in the class, as long as $this point out, you can change the value of a public variable, you can use the public variable in the function in the class. ......... Much has gone, and its application awaits you to find out.
Here is a very early article, suggest you can also see.
Current 1/2 page 12 Next read the full text

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.