Object-Oriented Programming

Object-oriented programming is a technique that solves problems by breaking them down into objects. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc.
Object-oriented programming is about creating objects that contain both data and methods, whereas Procedural programming is about writing procedures or methods that operate on data.

Classes and objects are the two main aspects of object-oriented programming:

  • Class
  • Object

First let’s start with the example of OOp after that we know more about it

Example :
Class = Fruit.
Object = Mango, orange ,Apple, Banana.
Other example :
Class = A specific car model, such as Audi A4, BMW I8, Maruti Suzuki etc.
Object = A specific car of any model like the car you own

Let’s learn a lot about classes and objects


Php class is group of value with set of operations to manipulate this values. Classes facilitate modularity and information hiding .classes are used to defined a new data type.

Some rules are here of class:

  • The class name can be any valid label.
  • It can not be reserved word
  • A valid class name starts with a letter or underscores, followed by any number of letter ,number or underscore.


class Classname
var $variable_name;
var $variable _name;
function Method_name()
body of Mathod
function Method_name(parameter_list)
Body of Method

Example of class:

class Model
var $model;
function showModel ($number)
$this-> model=$nunber;
echo "Number:$this->model";


Object is class type variable. Each time you create an object of class a copy of each variables defined in the class is the class is created .
In other words you can say that each object of a class has its own copy of data members of defined in the class.
Member function have only one copy shared by the all the objects of that class. All the Object may have their own value of variables.
The new Operator is used to create an object.


$object_name =new class_name ;

How to Access class member using object

->operator is used to access class menber using object .

Object _name->variable_name;


let’s have an example of Class and object

    class calculation   //--------------Class
        public $a,$b,$c;   //----------properties
        function sum()      //--------------method
            $this->c = $this->a+$this->b;
            return $this->c;
        function sub()
            $this->c= $this->a-$this->b;
            return $this->c;
    $c1=new calculation();//-------------object


    $c2=new calculation();


    echo"sum value of c1 :". $c1->sum() ."<br/>"; 
    echo"subtraction value of c2 :" .$c2->sub();

why use Object-Oriented Programming

  • Code more Modular and Reusable
  • Well Orginised code
  • Easier to debug
  • Best for medium to large website projects

Tutorial of Object-Oriented Programming in Php Programming