PHP (Core & Framework)

How to define and use class in PHP ?


A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}).

We define  all its properties and methods goes inside the braces. In a class, variables are called properties and functions are called methods. See the code below

class Student {
// Properties
  public $name;
public $section;

// Methods
  function set_name($name) {
$this->name = $name;
function get_name() {
return $this->name;

//The $this keyword refers to the current object, and is only available inside methods.
function set_section($section) {
$this->section = $section;
function get_section() {
return $this->section;
//Objects of a class is created using the new keyword.
$std = new Student();
$std ->set_name(‘Rajesh’);
$std ->set_section(‘A’);
echo “Name: “ . $std ->get_name();
echo “<br>”;
echo “Section: “ . $std ->get_section();

Comment here