التخطي إلى المحتوى الرئيسي

OOP'S in PHP

OOP'S in PHP, Class, and Object, Abstraction and Encapsulation, etc 


OOP'S means object-oriented programming structure, It is used to create a real-world based application using desktop and web technology.

OOP provides a set of rules to implement dynamic features in an application.


for example, if we want to provide security, dynamic memory allocation, code reusability, data accessibility option in your application then you should implement OOP's programming.



Rules of oop's                                                                             Features

class and object                                                         provide all features and it is mandatory

data abstraction and encapsulation                           security and accessibility

Polymorphism                                                          usability, overloading and overriding

Inheritance                                                               code reusability



 What is the class?


class is a blueprint of an object, it is user define datatype which contains data member and member function to define object characteristics and behavior.

 for example, Furniture is a class that will define all furniture type objects definition.

Without objects, we can not create real-time classes


.<?php


class Classname

{

   function functionname()

  {   

           Statements;

  } 


}


?>


What is Object?


It is a real-world entity that has identity, state, and behavior. object address will provide identity, memory provides state, and calling to data member and member function provide behavior.


Syntax for Object

<?php

$var = new  Classname();

?>


A real-time example of class and Object 


<?php


class Student

{

function sinfo($rno,$sname)

{

        echo "Rno is ".$rno." Name is ".$sname;


}




}


$obj = new Student();

$obj->sinfo(1001,"manish");

$obj1 = new Student();

$obj1->sinfo(1002,"manish kumar");



?>



Component of class:-

1) Data member:-  It is also called variable in PHP, it is used to define the attribute of Object.


1) Static Data Member:-  It will be called by class name and store data underclass memory.

static data members use static keyword to declare.

It will allocate one-time memory hence static provide constant memory not a constant value.but last value always will be store into memory.


static $var=value


it will be called by scope resolution operator using class name::


<?php


class StaticExample

{


   static $x=10;



}


StaticExample::$x=100;

echo StaticExample::$x;


?>



2)  The instance or Dynamic:-   it will allocate memory using an object, we can create unlimited individual memory to instance type data member.

<?php

class DynamicExample

{

var $x=10;

}


$obj = new DynamicExample();

$obj1 = new DynamicExample();

$obj1->x=200;

echo $obj->x;

echo $obj1->x;


?>


 


2) Member Function:-

It is used to define data members of the class because according to data encapsu7lation rules we should not access data members directly. 

Type of member function:-

1) static member function:-

It is used to define static data members of the class and called by class name.

<?PHP


class StaticExample

{


   static $x=10;

   static function display()

   {

    echo StaticExample::$x;

   }



}


StaticExample::display();



?>

2) dynamic member function:-

 it is used to define dynamic data members of the class and called by the object.

<?php


class DynamicExample

{


   var $x=10;

   function display()

   {

    echo $this->x;

   }



}


$obj = new DynamicExample();


$obj->display();



?>


3)  Constructor:-


It is used to initialize dynamic data members of the class, constructor name, and class name will be the same.

PHP also contain a default constructor


function __construct()

{


}


if this constructor is present then it will call.



Type of Constructor:-


1) Default:-  this constructor will be created by PHP Interpretar itself.



2)  User-defined:-  We will manually create a user-defined constructor.

<?php


class Consdemo

{

     function __construct()

     {

      echo "default";

     }

     /*function Consdemo()

     {

      echo "User define";

     }*/

      function Consdemo($a,$b)

     {

      echo "User define ".($a+$b);

     }


}



$obj = new Consdemo(2,2);


?>


4) Destructor:-  

It will be called when object will be destroyed means destructor is used to destroy the occupied memory of the object.


<?php


class Consdemo

{

    

     /*function Consdemo()

     {

      echo "User define";

     }*/

      function Consdemo($a,$b)

     {

      echo "User define ".($a+$b);

     }


     function __destruct()

     {

      echo "destructor will be called";

     }


}



$obj = new Consdemo(2,2);


?>




2)  Data Encapsulation & Data abstraction:-


Encapsulation means binding of data using a single unit. PHP provides a member function to define and declare all data member that is called encapsulation.

Binding all details under single unit is also called data encapsulation.


Abstraction means hiding of data for inaccessibility, class has essential details and nonessential detail to show only essentials details and hide all non-essential details is managed by data abstraction.


How we can implement data abstraction and encapsulation practically.


to implement data abstraction and encapsulation now I am creating a Bank class that contains all banking features credit, debit, check balance, and log in.


where login will be public all methods will be private which is called data abstraction using access specifier.


We will encapculate credit(), debit() and checkbalance() under login.

<?php


class Bank

{

  var $bal;

  function __construct()

  {

  $this->bal=5000;


  }

 private function credit($amt)

 {

    $this->bal+=$amt;

 }

private function debit($amt)

 {

    $this->bal-=$amt;

 }

private function checkbalance()

{

echo "Current balance is ".$this->bal;

}

public function login($pin)

{

   if($pin=1234)

   {

    $this->credit(12000);

    $this->checkbalance();

   }


}

}



$obj = new Bank();

$obj->login();

?>



note:-  we can also implement data abstraction using abstract class and interface.


4) Polymorphism:-  Poly means many and morphism means forms using this we can create a single name method or operator using multiple forms.

PHP not support overloading the concept, it only supports the overriding concept.

function overring will be implemented by Inheritance using this we can change the functionality of the old function to override from new function to create a new class.


if we want to call parent class constructor from child class constructor then we will use parent keyword.

<?php


class ExamSystem

{

function __construct()

{

echo "Parent";

}

function exampattern()

{

echo "Number System";

}



}


class ExamSystemNew extends ExamSystem

{

function __construct()

{

parent::__construct();

echo "Child";

}

function exampattern()

{

echo "Gradeing System";

}

}


$obj = new ExamSystemNew();

$obj->exampattern();


?>


5) Inheritance:-   it is used to provide reusability using this we can reuse the code of parent class to the child class. Inheritance provides a relationship between classes to access the data.


Type of Inheritance:-

1) Single Inheritance:-

We will inherit the features of the base class to the derived class.

Example

A  ---> B


2) Multilevel Inheritance:-

We will inherit the features of the base class to a derived class to a sub-derived class.

A ---> B ---> C

3)  Hierarchical Inheritance:-

It will provide inheritance using a tree structure, which means parent class features will be inherited by multiple child class.

                     A


B                                   C


Example of Inheritance:-


Now I am creating two different class Admin and Employee, now admin features will be inherited into employee class.


Example of Single Inheritance:-

<?php


class Admin

{

   var $id;

   var $name;

   function accept($id,$name)

   {

      $this->id = $id;

      $this->name=$name;


   }

   function display()

   {

    echo "ID is ".$this->id." name is ".$this->name;

   }



}


class Employee extends Admin

{

var $sal;

     function accept1($sal)

     {

         $this->sal=$sal;

     }

     function display1()

     {

      echo "Salary is ".$this->sal;

     }


}


$obj = new Employee();

$obj->accept(1001,"EMP");

$obj->accept1(12000);

$obj->display();

$obj->display1();


?>



Example of Multilevel Inheritance:-

<?php


class Admin

{

   var $id;

   var $name;

   function accept($id,$name)

   {

      $this->id = $id;

      $this->name=$name;


   }

   function display()

   {

    echo "ID is ".$this->id." name is ".$this->name;

   }



}


class Employee extends Admin

{

var $sal;

     function accept1($sal)

     {

         $this->sal=$sal;

     }

     function display1()

     {

      echo "Salary is ".$this->sal;

     }


}


class OtherStaff extends Employee

{

    var $bonus;

    function accept2($bonus)

    {

    $this->bonus=$bonus;

    }

    function display2()

    {

    echo "Bonus is ".$this->bonus;

    }


}


//$obj = new Employee();

echo "<br>Employee Information<br>";

$obj = new OtherStaff();

$obj->accept(1001,"EMP");

$obj->accept1(12000);

$obj->display();

$obj->display1();

echo "<br>OtherStaff Information<br>";

$obj1 = new OtherStaff();

$obj1->accept(1001,"OtherStaff");

$obj1->accept1(1000);

$obj1->accept2(500);

$obj1->display();

$obj1->display1();

$obj1->display2();

?>



Example of Hierarchical Inheritance:-

<?php


class Admin

{

   var $id;

   var $name;

   function accept($id,$name)

   {

      $this->id = $id;

      $this->name=$name;


   }

   function display()

   {

    echo "ID is ".$this->id." name is ".$this->name;

   }



}


class Employee extends Admin

{

var $sal;

     function accept1($sal)

     {

         $this->sal=$sal;

     }

     function display1()

     {

      echo "Salary is ".$this->sal;

     }


}


class OtherStaff extends Admin

{

    var $bonus;

    function accept2($bonus)

    {

    $this->bonus=$bonus;

    }

    function display2()

    {

    echo "Bonus is ".$this->bonus;

    }


}


//$obj = new Employee();

echo "<br>Employee Information<br>";

$obj = new Employee();

$obj->accept(1001,"EMP");

$obj->accept1(12000);

$obj->display();

$obj->display1();

echo "<br>OtherStaff Information<br>";

$obj1 = new OtherStaff();

$obj1->accept(1001,"OtherStaff");


$obj1->accept2(500);

$obj1->display();


$obj1->display2();

?>   



Abstract class and Interface in PHP:-

Abstract class:-

It is a special class in PHP which provides data abstraction using class, we can not create an object of an abstract class.


Abstract class contains a set of abstract methods and normal both. the abstract method contains only a declaration. because it will be used to declare a set of rules.


for example, if we purchase a car then we see all features of the car on paper.

the abstract class will work on the same pattern that contains all possible methods of the class.


Syntax of an abstract class:-


abstract class Classname

{

    abstract function functionname();

   functionname()

  {


  }

}


Note:-  all abstract methods should be implemented in class.if we not define then the program raise error it is called data binding between abstract class and normal class.


Example of Abstract class where we create two different classes one for Car and another for the customer.

the car class contains a prototype that will be implemented into the Customer class.



<?php



abstract class Car

{

function carinfo()

{

echo "Welcome in TATA MOTORS<br>";

}

     abstract function checkmilege();

      

     abstract  function checkspeed();


     abstract function checkprice();


}



class customer extends Car

{

   function checkmilege()

   {

      echo "milege<br>";

   }


    function checkspeed()

    {

          echo "speed<br>";

    }


    function checkprice()

    {

         echo "price<br>";

    }


}


$obj = new customer();

$obj->carinfo();

$obj->checkspeed();



?>

 

Interface:-


It is a pure abstract class in PHP because it contains only a set of abstract methods to declare a set of rules that will be implemented into class.


We can implement multiple inheritances using the interface.


The interface also provide data abstraction because we can not create an object of the interface.


Syntax of interface:-


interface Interfacename

{

   function functionname();


}


interface Interfacename2

{

  function functionname();


}



class Classname implemented Interfacename1, Interfacename2

{



}



note:-  Interface use implements keyword to implement the features of the interface to the class.


 

Example of Interface:-


<?php



interface Car

{

  function checkmilege();

      

      function checkspeed();


    function checkprice();


}


interface BankLoan

{

function emi();

function taxinfo();

function checkmilege();

}

class customer implements Car,BankLoan

{

   function checkmilege()

   {

      echo "milege<br>";

   }


    function checkspeed()

    {

          echo "speed<br>";

    }


    function checkprice()

    {

         echo "price<br>";

    }


    function emi()

    {


    }


    function taxinfo()

    {


    }

}


$obj = new customer();


$obj->checkspeed();



?>













 





PHP MOCK INTERVIEW SESSION OF DEVELOPER.






تعليقات

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...