Step by step learn about PHP of 5 classes and objects

  • 2020-03-31 20:19:17
  • OfStack

In this section, we'll look at how to create a class and object in PHP.

1. Create the class

Creating a class in PHP is similar to creating a class in C#/Java.
 
<?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 class of People, here are four points to explain:

The first is that in PHP, instead of accessing properties (or methods) with our usual point operator (.), we use -> .

The second is that in PHP, methods need to be identified by function, which is similar to Javascript.

The third point is that we need to use var when we declare a variable, which is very similar to Javascript.

The fourth point is that in PHP, there are also three access modifiers, public,protected and private, which are the same as C#.

Here, we find that we can directly access the $name attribute with $p, so we need to control it, the method is as follows:
 
class People 
{ 
private $name; 
public function GetName() 
{ 
return $this->name; 
} 
public function SetName($name) 
{ 
$this->name=$name; 
} 
} 

At this point, we cannot access the $name attribute from the outside.

Remember when we talked about variable functions? Here we can also use variable function to access the object method:
 
<?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 (properties)

Declaring static methods (also called class methods) in PHP is very similar to declaring static methods in C#.
 
<?php 
class DataBase 
{ 
public static function CreateConnection() 
{ 
echo("Success"); 
} 
} 
DataBase::CreateConnection(); 
?> 

The same is true for declaring static properties.
 
<?php 
class DataBase 
{ 
static $connectionString="http://127.0.0.1"; 
public static function CreateConnection() 
{ 
echo("Success"); 
} 
} 
echo(DataBase::$connectionString); 
DataBase::CreateConnection(); 
?> 

3. A class constant

In C#, we use const to identify constants, as we do in PHP.
 
<?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 properties (methods) belong to the class itself, and variables (methods) belong to the object itself, and the class itself exists before the object, so how do we access static variables (methods) in dynamic methods? In PHP, we have the self keyword.
 
<?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(); 
?> 

Related articles: