PHP is the scripting language for weak variables, which means you do not need to define them first. It can also give a lot of freedom, but for programs, freedom is not a good thing.
PHP is the scripting language for weak variables, which means you do not need to define them first. It can also give a lot of freedom, but for programs, freedom is not a good thing.
It brings a lot of trouble to protect staff browsing in the future.
Next we will enter 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 class, a simple class, with only one structure function. The goal 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 only used for authentication, but I think it is sufficient for php developers.
Next, describe what is the $ this object. From this point, we must first know what is a class.
Class, can be described as a group of similar groups, member variables in the class, the above $ hellow_str can be seen as a strange feature of this group, for example, the table group is
Class, the table has the desktop, the table foot, these "all have" things, is all the common 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 features, 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 we will explain.
For example, if we want to increase the area of a table, we will not target a group of tables, but the specific table to be rectified. this is an individual! Therefore, we need to "instantiate" this class for refined location. 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. $ D = $ this; do you understand that $ d is the specific table, $ d-> largen is used to zoom in on a specific table [all the others in the table group.
///////////////
Originally, $ this refers to a specific table. haha, I understand that the class is a collection of all attributes and methods, and a specific object, it is an individual in this group. since it is an individual in this group, it certainly has the common characteristics of the group and Fang Fara.
Member variables, member methods [member functions], instantiation, everyone knows.
But I want to increase my desk, but I still don't know how big it is. is that possible? what should I do?
Next, we will introduce structural functions. Let's talk about the structure function. let's just talk about it. he just determined the size of the table 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.
//////////
Intelligent you must want to instantiate it immediately. if I gave the value in the definition member variable at the beginning, 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, I'm sure the author is lying and clicking, bs, TT.
I am wrong. I know it is wrong. The function we just added
Function desktop (){
$ This-> desktop = 100;
$ This-> baluster = 100;
}
It is a structure function. what is a structure function? oh, the original structure function is a class-specific function. after the class is instantiated, the class will perform the structure function in the first step, it is opening up memory units for classes.
In order to verify whether it is implemented as soon as it is started, you should put your eyes on the first code in this tutorial. you can see a method with the same name as the class name, A structure function has output code after being instantiated. This confirms that I have not said anything wrong.
Well, back in our world of tables, you invented that at this time we had 100 of all the table faces and legs.
2. php
Fulfill code
//////////////
<?
Class desktop {
Var $ desktop; # table surface;
Var $ baluster; # table legs;
Function desktop (){
$ This-> desktop = 100;
$ This-> baluster = 100;
Echo 'table family commands: give me the same table face. '$ this-> desktop.' size! <Br/> ';
}
Function move (){
#....
}
Function largen (){
$ This-> desktop; # enlarge the table surface
Echo '7 ~, Small table I have changed today, don't worry about it, haha, I have a bigger face than you 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 me 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 'table family commands: give me the same table face. '$ this-> desktop.' size! <Br/> ';
}
Function move (){
#....
}
Function largen (){
$ This-> desktop; # enlarge the table surface
Echo '7 ~, Small table I have changed today, don't worry about it, haha, I have a bigger face than you 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 you are tired, you feel that you have evolved now. you should check the new type, invention ......
/////
$ D = new desktop (101 );
////
I am still a small tmd table.