What is OOPS ?
Object Oriented Programming is a methodology to build a program using classes and objects. It helps in building complex and reusable web applications.
The major concept of the object oriented programming in PHP was introduced from version 5. By using OOPS in PHP we can create modular web application.
Classes:
Classes are the blueprints for php objects. Class consists of properties(data/variables) and methods(functions) that forms a package called an object.

In OOPS terminology,
In a class data/variables are referred as properties and functions inside a class is referred as methods.

Now we will create a class having properties and methods.
<?php

class SimpleClass
{
    // property declaration in class
    public $var = 'default value';

    // method declaration in class
    public function simpleFun() {
        echo "Inside simple function";
    }
}

?>

                            

The above code will do nothing until and unless we create the object of a class.

  • To access the properties and methods outside of a class we have to create the object of class.
  • <?php
    
    class SimpleClass
    {
        // property declaration in class
        public $var = 'default value';
    
        // method declaration in class
        public function simpleFun() {
            echo "Inside simple function";
        }
    }
    
      $obj = new SimpleClass();
      echo $obj->var;
      echo "
    "; $obj->simpleFun(); /*   Output :   default value   Inside simple function */ ?>

  • To create an object out of a class, we need to use the 'new' keyword.

  • When creating/instantiating a class, we can optionally add brackets to the class name, as I did in the above example.

  • To access the properties and methods inside a class we use $this variable.

  • $this :

    '$this' is a special built in variable which is used to access the properties and other methods of current class.

    Constructor:

    Constructor is a built in method which is invoked/called when an object of class is created/instantiated.

    Encapsulation:

    In Encapsulation we set the access level of properties and methods. For this purpose we can use access level modifiers
    1. public:
      -> inside class
      -> outside class
      -> child class(means classes derived from that class)
    2. protected:
      ->inside class
      ->child class(means classes derived from that class)
    3. private:
      ->inside class
    Inheritance:

    When a class inherits all the properties and methods of base class then this is called inheritance.
  • We use extends keyword to inherit the parent or base class

  • To access the methods of parent class in child class we can use
    1. parent::foo();
    2. ClassName::foo();
    where :: is a scope resolution operator.

    Method Overriding:

    If a child class has the same method as declared in the parent class it is known as method overriding.
  • Methods must have same name and parameters as in parent class.

  • Method Overloading:

    Methods having similar signature yet have different parameters. In PHP we can only overload methods using the magic methods.

    final :

  • To prevent child class to override the method of parent we can define the method as final

  • Magic Constants:

    Magic Constants are written in uppercase letters and prefixed and suffixed with two underlines.
    1. __LINE__ : to get line no in which the constant is used.
    2. __CLASS__ : to get class name in which the constant is used.
    3. __FILE__ : to get the full path or file name in which constant is used.
    4. __METHOD__ : to get the name of method in which constant is used.

    Abstract Class :

    An abstract class is a class that has at least one abstract method.
  • abstract method can only have method name and parameters and no other code.

  • If we declare any method as abstract in a class then the class which extends that class should impliment that method

  • We can not create object out of abstract class.

  • Child class that inherits from abstract class must add bodies to the abstract methods.

  • An abstract class can have non abstract i.e concrete methods.

  • Interfaces :

    Interfaces is a next level of abstraction. Interfaces are similar to abstract class in the way that they provide abstract methods that should be implemented in the child class.
  • We use interface keyword for defining interface class.

  • interface class is implemented by child class using implements keyword

  • A child class can implement more than one interface class.

  • interface class can include abstract methods and constants but can not contain concrete methods and properties.

  • All the methods in the interface must be in public visibilty scope.
  • We can not create object out of interface class.

  • abstract vs interface :

    interfaces abstract class
    the code -abstract methods
    -constants
    -abstract methods
    -constants
    -concrete methods
    -concrete properties
    access modifiers public -public
    -protected
    -private
    number of parents the child class can implements more than one interface the child class can inherits only one abstract class
    About Me
    Rahul Yadav

    Rahul Yadav

    Technical Architect

    Kuala Lumpur, Malaysia

    Connect Me:     

    I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.

    I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.

    In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.

    Tag Cloud