I don't want to talk more about the power of OO. if you don't agree with OO, it may be very convincing if you look at the popular languages and do not support OO. In this section, we will look at how to create a class and object in PHP.
1. create a class
In PHP, the creation of a class is basically similar to that in C #/Java. let's take a look at the simplest example:
The code is as follows:
Class People
{
Var $ name;
Public function GetName ()
{
Return $ this-> name;
}
Public function SetName ($ name)
{
$ This-> name = $ name;
}
}
$ P = new People ();
$ P-> SetName ("kym ");
Echo ($ p-> GetName ());
?>
Here, we will create a People class, which has four key points:
The first is that in PHP, the access attribute (or method) is used instead of the commonly used vertex operator (.), but instead->.
The second is in PHP, the method must be identified by a function, which is similar to Javascript.
The third point is that when we declare a variable, we need to use var, which is similar to Javascript.
The fourth point is that in PHP, there are three access modifiers, namely public, protected, and private, which are the same as C.
Here, we found that we can use $ p to directly access the $ name attribute, so we need to control it as follows:
The code is as follows:
Class People
{
Private $ name;
Public function GetName ()
{
Return $ this-> name;
}
Public function SetName ($ name)
{
$ This-> name = $ name;
}
}
In this case, the $ name attribute cannot be accessed from outside.
Do you still remember the variable functions mentioned above? Here we can also use variable functions to access objects:
The code is as follows:
Class People
{
Private $ name;
Public function GetName ()
{
Return $ this-> name;
}
Public function SetName ($ name)
{
$ This-> name = $ name;
}
}
$ P = new People ();
$ Get = "GetName ";
$ Set = "SetName ";
$ P-> $ set ("kym ");
Echo ($ p-> $ get ());
?>
2. static methods (attributes)
Declaring static methods in PHP (also called class methods) is very similar to that in C.
The code is as follows:
Class DataBase
{
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
DataBase: CreateConnection ();
?>
Similarly, the declaration of static attributes is the same.
The code is as follows:
Class DataBase
{
Static $ connectionString = "http: // 127.0.0.1 ";
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
Echo (DataBase: $ connectionString );
DataBase: CreateConnection ();
?>
3. class constants
In C #, we use const to identify constants, which is also true in PHP.
The code is as follows:
Class DataBase
{
Const AUTHOR = "kym ";
Static $ connectionString = "http: // 127.0.0.1 ";
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
Echo (DataBase: AUTHOR );
Echo (DataBase: $ connectionString );
DataBase: CreateConnection ();
?>
4. access static variables
We know that static attributes (methods) belong to the class, while variables (methods) belong to the object, and classes exist before objects, so how can we access static variables (methods) in dynamic methods? In PHP, the self keyword is provided.
The code is as follows:
Class DataBase
{
Const AUTHOR = "kym ";
Static $ connectionString = "http: // 127.0.0.1 ";
Public static function CreateConnection ()
{
Echo (self: $ connectionString. "Success ");
}
}
Echo (DataBase: AUTHOR );
Echo (DataBase: $ connectionString );
DataBase: CreateConnection ();
?>