PHP Development Tutorial-object-oriented-PHP Tutorial

Source: Internet
Author: User
PHP Development Tutorial-Object-oriented. PHP is the scripting language for weak variables. that is to say, you do not need to define them first, which is flexible. It can also give a lot of freedom, but for programs, freedom is not a good thing. Since PHP is a scripting language with weak variables, you do not need to define them first, which is flexible. It can also give a lot of freedom, but for programs, freedom is not a good thing.

This is because it brings a lot of trouble for later maintenance personnel to read.

Next we will go to our first program:

1. php

////////////////////
<? Class helloWorld {
Var $ hellow_str = "Hello World! ";
Function helloWorld (){
Echo $ this-> hellow_str;
}
}
$ P = new helloWorld;
?>
////////////////////
Obviously, this is a very simple class with only one constructor. The purpose is to output "Hello World". First, we define a class member variable and output this variable through a special $ this object. I am not very refined in the oop idea, it is only used in php Development. Others are just for reference, but I think it is sufficient for php developers.

Next, explain what is the $ this object. From this point, we need to know what is a class.

Class, can be interpreted as a group of the same group, in the class member variables, the above $ hellow_str can be seen as a unique feature of this group, for example, the table group is
Class, the table has the desktop, the table foot, these "all have" thing, is the total characteristics of the table, in the program class we can define it as a member variable of the table class.

////////////
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
}
///////////
Similarly, in the world of tables, they also share common actions, such as moving [maybe you are pushing the table now ^], or we may want to enlarge the face of the table, in the same way, this is a common method proposed by the table group. [it is similar to the feature, but it is just an action.] Let's add this common feature to the class.

////////////////
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function move (){
#....
}
Function largen (){
#...
}
}///////////////
Now that you know the above, you can understand what the $ this object is. no error occurs. it is a specific object in the table group. if you still cannot understand it,
Next, let's explain.

If we want to change the area of a table, for example, if we want to increase the area of the table, we are not targeting a group of tables, but the specific table to be rectified. this is an individual! Therefore, we need to "instantiate" this class for exact positioning. We don't need to make any changes to other tables.

////////////////
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function move (){
#....
}
Function largen (){
$ This-> desktop ++; # enlarge the table surface
}
}
$ D = new desktop; # "instantiate. at this time, we pull a specific table!
$ D-> largen (); # Haha, zoom in on a specific table. In fact, $ d = $ this; do you understand that $ d is the specific table, $ d-> largen is to enlarge a specific table by using the specific table [methods for increasing the number of table groups.
///////////////


It turns out that $ this refers to a specific table. haha, I understand that the original class is a set of attributes, methods, and a specific object, it is one of these groups. since they are individuals, they certainly possess the common characteristics and square Farah of the group.

Member variables, member methods [member functions], instantiation, everyone knows.

But even though I want to enlarge my table, I still don't know how big it is. is that possible? what should I do?

Next, we will introduce the constructor. Let's talk about constructor. let's just talk about it. he just sizes the table surface and legs.

////////////////
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function desktop (){
$ This-> desktop = 100;
$ This-> baluster = 100;
}
Function move (){
#....
}
Function largen (){
$ This-> desktop ++; # enlarge the table surface
}
}
////////////
I have defined the table size and length.
//////////
Clever, you must want to instantiate it immediately. if I start to give the value in the definition member variable, isn't it that all the tables are as big?

//////////////
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function desktop (){
$ This-> desktop = 100;
$ This-> baluster = 100;
}
Function move (){
#....
}
Function largen (){
$ This-> desktop ++; # enlarge the table surface
}
}
$ D = new desktop;
$ D-> desktop ();
////////////////////
But the result is: Call to a member function desktop () on a non-object in

Unfortunately, it must have been a scam by the author, bs, TT.

Actually, I am wrong. I know it is wrong. The previously added function

Function desktop (){
$ This-> desktop = 100;
$ This-> baluster = 100;
}
It is a constructor. what is a constructor? oh, the original constructor is a class-specific function. after the class is instantiated, the class will automatically execute the constructor in the first step, it is opening up memory units for classes.

In order to verify whether it is executed at the beginning, you should take a look at the first code in this tutorial. you can see that a method with the same class name, a constructor, the code has been output after we instantiate it. This proves that I have not said anything wrong.

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

2. php

Execute code

//////////////
<?
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function desktop (){
$ This-> desktop = 100;
$ This-> baluster = 100;
Echo "we have commands for the table family. you can give me all the table faces in the same way.". $ this-> desktop. "size! <Br/> ";
}
Function move (){
#....
}
Function largen (){
$ This-> desktop ++; # enlarge the table surface
Echo "7 ~, I have transformed the small table today. Don't worry about it. haha, my face is bigger than yours now. I have ". $ this-> desktop." Ga ";
}
}
$ D = new desktop;
$ D-> largen ();
?>
Small table, escaped from the table family, because our face is not small table, this nb is big. The elders at the table couldn't see it anymore. look at me, look at me.

//////////////
<?
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function desktop ($ desktop ){
$ This-> desktop = $ desktop;
$ This-> baluster = 100;
Echo "we have commands for the table family. you can give me all the table faces in the same way.". $ this-> desktop. "size! <Br/> ";
}
Function move (){
#....
}
Function largen (){
$ This-> desktop ++; # enlarge the table surface
Echo "7 ~, I have transformed the small table today. Don't worry about it. haha, my face is bigger than yours now. I have ". $ this-> desktop." Ga ";
}
}
$ D = new desktop;
$ D-> largen ();
?>
Small table, turn left, turn right, turn around and watch it for a long time. when I am tired, I feel like I have evolved. it's time to look at my new kind, found ......

/////
$ D = new desktop (101 );
////
I am still a small tmd table.

Bytes. It can also give a lot of freedom, but for programs, freedom is not a good thing. Because after giving...

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.