Comments

Thursday, April 19, 2012

Object-Oriented Programming meets PHP with examples

Posted by on Thursday, April 19, 2012 Read our previous post
After getting request for the above topic, we decided to write a tutorial on it. However, if you need more detailed lessons you can check out www.computerclasses.org for available courses on this topic.

As we all know that object-oriented programming is an approach in which objects and classes are used and this approach is being followed by the some of the leading programming language like C++ and Java. Click here, to read about OOPs Concept in Java.

Before the introduction of PHP 4 OOP in PHP was not at all possible but after its introduction in PHP 4 and more advancement in PHP 5, the users are making full use of it and other programmer who thought PHP to be difficult are now actually enjoying it.



Class in PHP


Like in java a class defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes or properties) and the things it can do (its behaviors or methods or features). Similarly in PHP a class is the script that serves as a template, or the pattern, that is used to create an object.

Before we go ahead with a new Example with codes i would like to describe a new concept called $this variable.

The $this variable is a special variable that refers to the property of the same class. It can't be used outside a class.

The format fro using $this variable is :

$this->varname;


Example :

<?php
class Car{

    public $color;
    public $tires;
    public $price;
    public $gas;

    public function __construct(){
       echo 'About this Car.<br />';
    }

    public function showPrice(){
       echo 'This car costs $'.$this->price.'.<br />';
    }

    public function amountOfGas(){
       echo 'This car has '.$this->gas.' amount of Gas left.<br />';
    }

}
?>

Object in PHP


Basic elements of object-oriented programs are objectsAn object is a particular instance of a class. The class of Dog defines all possible dogs by listing the characteristics that they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur. In programmer jargon, the object Lassie is an instance of the Dog class. The set of values of the attributes of a particular object is called its state.

As in other programming language that support OOP, objects have properties, also called as attributes. Ex. color, size, speed of car.

Here also the new keyword is used to create an object of some class.


<?php

$carObj = new Car; //new object of Class Car created, and constructor is called

$carObj->color = 'Red';

$carObj->gas = 20;
$carObj->price = 200000;
$carObj->showPrice();
$carObj->amountOfGas();

?>


Methods in PHP


Methods use the same syntax as functions.The only difference is that they are functions defined inside a class. Thus, they can't be called independently of an object.

Inheritance


Inheritance is nothing but a process by which an object acquire a property of another object.

Example :

Suppose you have two car objects: one with white cars and one with red cars. You could write two classes: a redCar class and a whiteCar class. However, a lot of the information is the same for both objects. Both are Car, both have tires, and both run on gas. Inheritance enables you to eliminate the duplication.

Like in Java or C++, where we have concept of parent and child class, here also in PHP we have concept of Parent and Child Class. Ex. In the above example you can define a class called Car which has common information like  $gas - 0, $tires - MRF. Now we can have subclasses i.e. redCar and whiteCar can be called as child classes, which inherit the property of the parent class.


<?php

/*** include the Car class definition ***/
include('car.class.php');

class SUV extends Car{

    /*** the number of side cars ***/
    private $color;
    private $make;
    public function setMake($make){
      $this->make=$make;
    }

    public function setColor($Color){
      $this->color = $Color;
    }
}

/*** create a new Car object ***/
$carObj = new SUV;
$carObj->price = 1500000;
$carObj->color = 'Black';
$carObj->setMake('BMW');

echo $carObj->make.': '.$carObj->color.'<br />';
$carObj->showPrice();
?>


Note :

PHP omits concept of polymorphism and multiple inheritance.

For more tutorial on PHP tutorial and OOPs Concept, subscribe to us

© 2010 Code 2 Learn