In this section, let's take a look at how to create a class and object in PHP.
1. Create Class
In PHP, create a class similar to the one in C#/java, gossip less, and show you the simplest example:
Copy Code code 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'll create a people class, here are four points to illustrate:
The first is that in PHP, access to properties (or methods) is not in the use of the dot operator (.) we use, but rather with->.
The second is in PHP, where the method needs to be identified with a function, which is similar to JavaScript.
The 3rd is that when we declare a variable, we need to use Var, which is very similar to JavaScript.
The 4th is in PHP, also has public, protected,private three and C # the same access modifier, no longer repeat.
Here, we find that we can use $p to access the $name property directly, then we need to control it, the following methods:
Copy Code code as follows:
class people
{
Private $name;
Public Function GetName ()
{
return $this->name;
}
Public Function SetName ($name)
{
$this->name= $name;
}
}
At this point, we can not access the $name properties outside the world.
Remember that we mentioned the variable function in the above? Here we can use the variable function to access the object's method:
Copy Code code 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 method (properties)
Declaring a static method (also known as a class method) in PHP is very similar to C #.
Copy Code code as follows:
<?php
Class DataBase
{
public static function CreateConnection ()
{
Echo ("Success");
}
}
Database::createconnection ();
?>
Similarly, declaring static properties is the same.
Copy Code code 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, as in PHP.
Copy Code code 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. Accessing static variables
We know that static properties (methods) belong to the class itself, and that the variables (methods) belong to the object itself, and that the class itself exists before the object, how do we access the static variable (method) in the dynamic method? In PHP, we provide the Self keyword.
Copy Code code 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 ();
?>