Classes in PHP .

A class is a unit of programmer-defined data type that describes the characteristics and behaviors of something, or of the group of things.A class includes local functions as well as local data

For Example A class called Car, would describe the characteristics and behaviors common to all cars.

A PHP Class has 3 main elements.
PHP Data Member:Data members are also variable but difference is that they are in encapsulated form.
PHP Property:A property is a variable inside the class which can contain some information. Like Name is the property of student class.
PHP Method:Methods are functions inside the body of class, methods can also be accessible by those three types of users.

Syntax : To create a class, use PHP’s “class” keyword.

Example :<?php

class Student ///////PHP Deceleration of class


public $name; /////////PHP public property

public $age;

public function set($name1,$age1) //////////PHP Method Or Function


$this->name = $name1; ////////Note: Do not use $ symbol in front of properties.

$this->age = $age1;


Public function get()


echo $this->name;
echo $this->age;


$obj =new Student(); /////////Creating Object

$obj->set("bhopal",60); ///////// Calling Class Function SET and passing value

echo $obj->get(); ///////// Calling Class Function GET


One thought on “Classes in PHP

Comments are closed.