Monday, April 1, 2019

What is the role of object oriented programming language (Oops) in php and why used it in php.

Object Oriented Programming, or OOP, refers to the method of programming that invokes the use of classes to organize the data and structure of an application. Mostly all php Framework based on Object Oriented Programming.

Why use Object Oriented Programming in php :

  • Code Maintainability
  • Code Re-usability
  • Abstraction
  • Molecularity

    What are the must learn Object Oriented Programming concepts in PHP?
  • Class − This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.example :
       Class MyClass{
                       / / my code
           }
            
 
  • Object − An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.For example:
     
          $object = new car();
  • Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.For example:

           class Parent
                  {
                         // The parent’s class code 
                   } 
                            class Child extends Parent
                  { 
                        // The child can use the parent's class code 
                   }


  • Polymorphism − This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it make take different number of arguments and can do different task.For example:

    class Circle implements Shape {

  private $radius;
   
  public function __construct($radius)
  {
    $this -> radius = $radius;
  }
  
  // calcArea calculates the area of circles 
  public function calcArea()
  {
    return $this -> radius * $this -> radius * pi();
  }
}
  • Overloading − A type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.For example:

class Foo {
   function myFoo() {
      return "Foo";
   }
}

class Bar extends Foo {
   function myFoo() {
      return "Bar";
   }
}



  • Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).For example

    abstract class Mobile
    {
    public Calling(); // calling function
    public SendSMS(); // calling function
    }

  • Encapsulation − refers to a concept where we encapsulate all the data and member functions together to form an object.

    class Redbag{
    Pencil(); // calling function of pen
    readBook(); //calling function of read book
    }

  • Constructor − refers to a special type of function which will be called automatically whenever there is an object formation from a class.


    class Bird
    {

    public $bird_name = "No any birds for now";

    public function __construct($bird_name)

    $this->bird_name = $bird_name;
    }
    }

    $bird = new Bird("Sparrow is bird"); // now constructor is called automatically because we have initialized the object or class Bird.
    echo $bird->bird_name;


    Output of above code is :
    Sparrow is bird
    • Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.
      class Bird { public $bird_name = "No any birds for now"; public function __construct($bird_name) { echo "I'm alive!"; $this--->bird_name = $bird_name; } public function __destruct() { echo "I'm dead now :("; } } $bird = new Bird("Sparrow is bird"); echo "Name of the bird: " . $bird->bird_name; ?> Output : I'm alive!Name of the bird: Sparrow is birdI'm dead now :( // so as we seen, __destruct is called when object is removed or destroyed.