PHP Class and Object

Author: Al-mamun Sarkar Date: 2021-04-21 10:32:36

Class is a user-defined data type that has properties and methods and the object is the instance of the class. In this lesson, I will show you how to create a class and object of a class. 

 

Syntax:

class ClassName {
  // codes for the class
}

 

Creating a Class:

class Vehicle 
{
	public $name;

	function setName($name) 
	{
		$this->name = $name;
	}

	function display() 
	{
		echo "Vehicle Name is : {$this->name} <br/>";
	}

	public function getVehicleName() 
	{
		return $this->name;
	}
}

 

Create and Access Object of a Class:

$obj = new Vehicle();
$obj->name = "Car";
$obj->display();
print($obj->getVehicleName());