Learn more about the concept of PHP class Class

  • 2020-05-17 04:56:47
  • OfStack

For example, a vehicle can be defined as having properties such as color, number of tires, manufacturer, model and capacity, and behaviors such as stopping, advancing, turning, and honking. In OOP terms, the concrete definition of an entity's nature and behavior is called a class (class).

Class definition and creation
A class is a collection of 1 group of objects that have the same properties and services. It provides a uniform abstract description of all objects that belong to the class, with two main parts inside: properties and methods. In object-oriented programming languages, a class is a separate unit of program that should have a class name and include two main parts: a property description and a method description.

Class is used to represent the actual things to be processed in the application. For example, if you want to create an application that manages a public library, you might want to include classes representing books, magazines, employees, special events, customers, and other things that need to be managed. Each entity contains a set of properties and behaviors, known in OOP as fields (field) and methods (method), which define entities. The 1-like class creation syntax in PHP is as follows:

 
class Class_Name 
{ 
//  Field declarations  
//  Method statement  
} 

Create 1 class:
 
class Employee 
{ 
private $name; 
private $title; 
protected $wage; 

protected function clockIn() { 
echo "Member $this->name clocked in at ".date("h:i:s"); 
} 

protected function clockOut() { 
echo "Member $this->name clocked out at ".date("h:i:s"); 
} 
} 

This class, called Employee, defines three fields: name, title, and wage, as well as two methods: clockIn (check in) and clockOut (check out).

The application of class
A class that defines properties and methods is a complete class, and can contain a complete processing logic in a class. Use the new keyword to instantiate an object to apply the logic in the class. Multiple objects can be instantiated simultaneously.

Class instantiation:
 
object = new class_name(); 

After instantiating 1 object, use - > Operator to access the member properties and methods of the object. Such as:
 
object->var_name; 
object->function_name; 

If you want to access a member's properties or methods in a defined class, you can use the pseudo-variable $this. $this is used to represent the current object or the object itself.
 
<?php 
class Person { 
//  The member attribute of a person  
var $name; // The name of the person  
var $age; // A person's age  

// Members of the people  say()  methods  
function say() { 
echo " My name is: ".$this->name."<br />"; 
echo " My age is: ".$this->age; 
} 
} 
// End of class definition  

$p1 = new Person(); // instantiation 1 An object  
$p1->name = "Gonn"; // to  $p1  Object property assignment  
$p1->age = 25; 
$p1->say(); // In the calling object  say() methods  
?> 

Program running results:
 
 My name is: Gonn 
 My age is: 25 

Related articles: