Object-Oriented Programming In Php

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

Class

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.

Syntax:

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

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 objects may have their own value of variables.

The new Operator is used to create an object.

Syntax:

$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;
$samsung->model;

Object_name->method() 
$car->Model();
 
Object_name->method_name(parameter_list)
$car->Model('BMW');

let’s have an example of Class and object

    <?php
    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

    $c1->a=20;
    $c1->b=10;

    $c2=new calculation();

    $c2->a=20;
    $c2->b=10;

    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

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x