The object-oriented _php techniques for the Introduction to PHP development tutorial

Source: Internet
Author: User
PHP is a scripting language for weak variables, which means you don't have to define them first, which is very flexible. can also give a lot of freedom, but for the program, freedom is not good.

Because to give the later maintenance staff to read a lot of trouble.

Here we go to our first program:

1.php

////////////////////
var $hellow _str = "Hello world!";
function HelloWorld () {
echo $this->hellow_str;
}
}
$p = new HelloWorld;
? >
////////////////////

Obviously, this is a class, a very simple class, with only one constructor. The goal is to output "Hello world"; First we define a class member variable, through a special $this object, output the variable, oop thought I am not very fine, just in PHP development, the other is only used for reference, but I think for PHP developers is enough.

Next, explain what the $this object is, and from this point we first need to know what the class is.

class, can be interpreted as a group of the same group, the member variables in the class, such as the above $hellow_str can be seen as the unique characteristics of this group, for example, the table group is a
class, tables have desktops, table legs, these "all have" things, is a common feature of the table, in the program class we can define it as a member of the table class variable.

////////////
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
}
///////////

Similarly, in the world of tables, they also have a common action, such as moving [maybe you are now pushing the table ^ ^], or maybe we want to enlarge the face of the table, the same, this is the table group proposed a common method, [and features approximate Oh, but this is the action only]. Let's add this common feature to the class.

////////////////
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
function Move () {
#....
}
function Largen () {
#...
}
}///////////////

Know the above, then it is very good to understand what is the $this object, there is no fault, he is a table in the group of a specific object, if you do not understand, then,
Let's go ahead and explain.

If we are going to make a correction to a table now, for example, we want to make the table larger, then we are not targeting a group of tables, but the specific table to be corrected, this is an individual! So, we're going to "instantiate" this class for precise positioning. Because, we don't need to rectify the other tables.

////////////////
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
function Move () {
#....
}
function Largen () {
$this->desktop++; #放大桌子面
}
}
$d = new desktop;# "Instantiation, this time we are for a specific table pull!
$d->largen (); #哈哈, enlarge this particular table surface. In fact, $d = $this; You see, $d is that particular table, $d->largen is using that particular table [the table group has all the big ways] to zoom in on a particular table.
///////////////


Originally, $this refers to a specific table, haha, understand, the original class is all the attributes, methods of collection, and a specific object, is a group of individuals, since they are the individual, of course, the group has the common characteristics and methods of pulling.

member variables, member methods [member functions], instantiation, we all know.

But although my table wants to be bigger, but I still do not know how big the table starts, this is possible, how to do?

Next, we rolled out the constructor to pull. When it comes to the constructor, it's not much to say, he just gives us the table and table legs to determine the size.

////////////////
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
function Desktop () {
$this->desktop = 100;
$this->baluster = 100;
}
function Move () {
#....
}
function Largen () {
$this->desktop++; #放大桌子面
}
}
////////////
See, I've defined the size and length of the table.
//////////

Smart you, you must want to instantiate immediately, if I start in the definition of member variables to give value, that is not to say that all tables are the same size?

//////////////
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
function Desktop () {
$this->desktop = 100;
$this->baluster = 100;
}
function Move () {
#....
}
function Largen () {
$this->desktop++; #放大桌子面
}
}
$d = new Desktop;
$d->desktop ();
////////////////////

But the result is: call to a member function desktop () on a non-object in

Unlucky, must be the author in the Cheat Click, BS this demo, TT.

In fact, I was wrong, I know wrong. The function we just added

function Desktop () {
$this->desktop = 100;
$this->baluster = 100;
}

is a constructor, what is a constructor, alas, the original constructor is a class-specific function, and after the class is instantiated, the class will automatically execute the constructor, which opens up the memory unit for the class.

To verify that it was not executed at the outset, please put your eyes on the first code of this tutorial, quack, you see the put, A and class name the same method, a constructor, after we instantiate the output code, it proves that I have not said wrong.

Well, back to our table world, you find that all the tables and legs are 100 at this time.

2.php

Executing code

//////////////
?
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
function Desktop () {
$this->desktop = 100;
$this->baluster = 100;
echo "Our table family has ordered everyone to give me the table face." $this->desktop. Size!
";
}
function Move () {
#....
}
function Largen () {
$this->desktop++; #放大桌子面
echo "7~, small Table I changed today, do not bird you pull, haha, I now face can be bigger than you, I have." $this->desktop. " Quack ";
}
}
$d = new Desktop;
$d->largen ();
? >

Small table, fled the table family, because, our face no small table this NB big. The old elders of the table can not see it, small sample, look at me.

//////////////
?
Class desktop{
var $desktop; #桌子面;
var $baluster; #桌子腿;
function Desktop ($desktop) {
$this->desktop = $desktop;
$this->baluster = 100;
echo "Our table family has ordered everyone to give me the table face." $this->desktop. Size!
";
}
function Move () {
#....
}
function Largen () {
$this->desktop++; #放大桌子面
echo "7~, small Table I changed today, do not bird you pull, haha, I now face can be bigger than you, I have." $this->desktop. " Quack ";
}
}
$d = new Desktop;
$d->largen ();
? >

Small table, turn left turn, turn right to look for a long time, stroll tired, feel oneself now also evolved, should see the new same kind of so; after the table changed body execution, found ...

/////
$d = new Desktop (101);
////

I'm still a little table in the TMD.

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.