PHP OOPs tutorial Part2

 OOPs Basics in PHP


Part1

Table of Contents

PHP Object-Oriented Programming Part2

 

 

  

Inheritance in PHP:

Basically inheritance is a mechanism where a new class is derived from the existing base class.
The derived class acquires the functionality of the base class. To extend the class behavior,  a keyword called “extends“ is used.

Access Specifiers specify the level of  accessibility for other objects and functions that have on the data members / member functions of the class.

In Inheritance, the derived class can access the base class attributes having protected/public access specifier.

To access private data of a base class from a derived class, you  need to  call public/protected method of the base class, which will access the private variable and return the data to the derived class.

In PHP only single inheritance is allowed. i.e. a derived class can inherit only one class at a time.


Example 1 – Basic Inheritance Call

<?php

class Baseclass
{
    protected $firstname = "John";
    protected $lastname = "Doe";
}
class Childclass extends Baseclass
{
function __construct()
{
    echo $this->firstname;
    echo $this->lastname;
}
}
    $a = new Childclass();
?> 
 
Output: John Doe
 

 

Example 2 : Accessing Private Data Member through Inheritance

<?php
class Baseclass
{
    private $firstname = "John ";
    protected $lastname = " Doe";
 
protected function getData()
{
    return $this->firstname;
}
}
class Childclass extends Parentclass
{
function __construct()
{
    echo $this->getData();
}
}
    $a = new childclass ();
    $a->getData();
 
?>

Goto ↑

 

Polymorphism in PHP

Polymorphism in PHP  is a technique where the method  is invoked based on the class object calling it at runtime.
The basis of Polymorphism is Inheritance and function overriding.

Function Overriding

Refers the base class and the derived class methods with same name, signature, and access specifier.

Example1 :  Basic Polymorphism

 
<?php
 
class BaseClass
{
public function myMethod()
{
    echo "BaseClass method called";
}
}
 
class DerivedClass extends BaseClass
{
public function myMethod()
{
   echo "DerivedClass method called";
}
}
    //$c = new BaseClass();
    $c = new DerivedClass();
    $c->myMethod();
?>
 

Output: DerivedClass method called.

 
 

In the above program, there are two classes, a BaseClass and a DerivedClass.   

But using a Derived Class Object, derived class method (myMethod()) is called.  

Then a DerivedClass version of method get called.

 

 

Example 2 -  Function Overriding

 
<?php
 
class Person
{
function printAge($dob)
{
    echo "<br> ". $dob;
}
}
 
class Customer extends Person
{
function printAge($dob)
{
    echo "<br> ". $dob;
} 
 
}
    $c1 = new Customer();
    $p1 = new Person();
 
    $c1->printAge("some Data");
    $p1->printAge("somemore Data"); 
 
?>
 
Output:
some Data
some more Data
 
  • In the above program there are  two classes Person and Customer
  • Customerclass is extending the Person class
  • Both the classes having method printAge() defined, 
    There two objects $c1,  $p1. $p1->printAge() calls parent method and
    $c1->printAge() calls customer printAge method 
 
 



Move ↑

Parent Keyword in PHP

In PHP  Parent keyword allows calling the base class method
not the derived class method, which is called by default with child class object.
This is useful while implementing Polymorphism in PHP. 

Parent keyword is also very useful if you want the derived class to call the
parent class before performing certain operation.
 

 

Example – Using Parent Keyword

 

<?php
class Person
{
public function showData()
{
echo "This is Person's showData()";
}
}
class Customer extends Person
{
public function showData()
{
parent::showData(); //Calling Person Class showData Method
echo "This is Customer's showData()";
}
}
$c = new Customer();
$c->showData();
?>

Output:
This is Person’s showData()
This is Customer’s showData()

 

In the above code, there are two classes, Person  and  Customer.

Customer Class extending the Person Class.
Customer Class showData() method is calling the Parent class showData() method.

 

 

Static Keyword in PHP

The static methods/attributes are available as a part of the class. So it is available to all the objects defined for the class. To implement static keyword functionality to the attributes or the methods will have to be prefix with static keyword. To assign values to static variables you need to use scope resolution operator(::) along with the class name.

Example:

<?php
class StaticDemo
{

static private $i;             //Defining Static Variable

function __construct()
{
//Accessing Static Variable for incrementing
 
ClassName::$i++;     

echo "<br>". ClassName::$i;    //Accessing Static Variable for printing

}
}
$a = new StaticDemo();
$b = new StaticDemo();
$c = new StaticDemo();
?>

Example2:

<?php
class StaticExample
{
static private $i; //Defining Static Variable
function __construct($value)
{
    if($value != "")
    {
    ClassName::$i= $value; //Accessing Static Variable
    }
    echo "<br>". ClassName::$i; //Accessing Static Variable
 
    $a = new StaticExample("10");
    $b = new StaticExample("20");
    $c = new StaticExample(""); 
 
?>
 
Output:
10
20
20
Explanation:
  • In above program, declared static variable $i.

  • In the constructor, checking the variable is empty or not and  then assigning the value to the static variable.

 


 ↑

Using Interface in PHP

An Interface is similar to a class with a set of empty methods with no implementation. 

 

Any class that inherits from an interface must implements the missing member function body.

 In PHP class may inherit only one class, but because interfaces lack an implementation any number of class can be inherited.

In PHP, interfaces may declare only empty methods. An interface cannot declare any variables. To inherit from an Interface, keyword implements is used. One class can extend more than one interface.

Syntax:
interface interfacename
{
    function fname();
    function lname();
}
 
Example:
<?php
interface interface1
{
    function fname();
    function lname();
}
class temp implements interface1
    public $fname="bill";
    public $lname="gates";
 
public function fname()
{
    echo "<br>".$this->fname;
}
public function lname()
{
    echo "<br>".$this->lname;
}
}
$a = new temp();
$a->fname();$a->lname();
?> 

Example2 Interface ,implementing Class in PHP

<?php
 
interface employee
{
function setdata($empname,$empage);
function display();
}
class Payment implements employee
{
function setdata($empname,$empage)
{
     ;
}
function display()
{
    echo "<br>Payment methods";  
}
}
    $a = new Payment("aa", "bb");
    $a->display();
?>
Output: Inside Payment Class

In the above Example an interface class called employee having two methods  setData()and display()

Interface implemented by Payment class  and provided the functionality for the methods defined in the interface.

Example3 Combining Abstract Class and Interface Class

<?php
 
interface employee
{
    function setdata($empname,$empage);
    function displayData();
}

abstract class Salary implements employee

{
abstract function salaryDetails();
}
 
class PaySlip extends Salary
{
function collectPaySlip()
{
    echo "Payment Recived";
    $this->displayData();
} 
 
function setData($empname,$empage) //interface method1
{
//Functionality
} 
 
 
function displayData() //interface method
{
    echo "<br>Inside PaySlip Class";
}

 
function salaryDetails() //abstract class method2
{
    echo "Inside PaySlip Class";
}
 
}
    $a = new PaySlip();
    $a->collectPaySlip();
?>
 
Output: 
Payment Received
Inside Payment Class

Goto ↑

Abstract Class in PHP

What is an Abstract Class?

An abstract class  means a partially implemented class. It contains attributes and member functions, but some members functions are not defined. They are empty methods. Only method declaration is provided not the implementation, and is waiting for some other class to extend it through inheritance so that the derived class provides a full functionality for the incomplete methods.
An abstract class cannot be instantiated, and it can only be extended.
An abstract class prefixed with the keyword 'abstract'. And the method which not defined in abstract class also prefixed with 'abstract'. If a method is defined as an abstract, then it is always public or protected. Cannot be a private.



Syntax:
 
 
<?php
 
abstract class absclassname
{
//attributes and methods
.
.
abstract function methodname;  //abstract method 
}
 
class derived extends absclassname
{
    function methodname(){} 
 
?>

Example 1 - Basic Abstract Class Usage

<?php
 
abstract class Employee
{
    protected $empname;
    protected $empage; 
 
function setdata($empname, $empage)
{
    $this->empname = $empname;
    $this->empage = $empage;
}
 
abstract function display();   //abstract method 
 
}
 
class Employee2 extends employee //extending abstract class
{
function __construct($name, $age)
{
    $this->setdata($name, $age);
}
function display()
{
    echo $this->empname;
    echo $this->empage;
}
}
    $a = new Employee2("Jimmy","23");
    $a->display();
?>
 
Output:
Jimmy
23

 Explanation for the Example 1:

Employee class is an abstract class. It  has 2 data members 

  • $empname and 
  • $empage. 

It also has an abstract method display() and public method setData().
Now the Employee class is extended by Employee2. It contains the functionality for the
abstract method display() defined in employee class.


Example 2 Abstract Class Error 

 

Abstract method display() defined in employee class and must be defined in child class Employee2. If  not defined, then  Employee2 is not able to create an object because it is ignoring empty method display() implementation.

 
<?php
 
abstract class employee
{
    protected $empname;
    protected $empage; 
 
function setdata($empname, $empage)
{
    $this->empname = $empname;
    $this->empage = $empage;
}
 
abstract function display(); //should be implemented in child class
 
} 
 
 
 
class Employee2 extends employee
{
function __construct($name,$age)
{
    $this->setdata($name,$age);
}
}
$a = new EmployeeData("John","24");
 

 

//Will generate an error as the Employee2 class doesn't define the abstract method outputData

?>

Output This will give you an error 


Class Employee2 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Employee::display() )” 

 

** should be implemented as follows in Employee2 class  

 
 
function display()
{
    echo "<br />empname".$this->empname;
    echo "<br />empage".$this->empage;
}

 

 

Example3 - Multiple Abstract Classes

 
<?php
 
abstract class employee
{
    protected $empname;
    protected $empage; 
 
function setdata($empname,$empage)
{
    $this->empname = $empname;
    $this->empage = $empage;
}
abstract function display();
}
 
abstract class Employee2 extends employee //class1
{
 
    abstract function display(); 
}
 
class Payment extends Employee2 //class2
{
 
function display()
{
     echo "Inside Payment Class <br>";
 
     echo $this->empname ;
     echo $this->empage ;
 
 
 
 } 
 

}
    $a = new Payment();
   
    $a->setData("aaa","bbb");  
    $a->display(); 
?>
 
Output: Inside Payment Class

 aaa bbb

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP