PHP Class and Object

PHP Class and Object

Instructor-svg Al-Mamun Sarkar
Apr 21 , 2021

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());

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram