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:
Copy codeThe Code is as follows:
<? Php
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:
Copy codeThe 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:
Copy codeThe Code is as follows:
<? Php
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.
Copy codeThe Code is as follows:
<? Php
Class DataBase
{
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
DataBase: CreateConnection ();
?>
Similarly, the declaration of static attributes is the same.
Copy codeThe Code is as follows:
<? Php
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.
Copy codeThe Code is as follows:
<? Php
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.
Copy codeThe Code is as follows:
<? Php
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 ();
?>