Object Oriented Programming Concepts

Terminologies involved

  • Class
  • Object/instance
  • Properties & methods

Introduction

  • OOP is a style of programming that allows you to create or define your own data structure. Here, newly created data structure can represent anything that your application requires. For example; Form, Database, Product, Customer.
  • OOP is centered around objects rather than functions (as in POP).

4 Principles of Object Oriented Programming

Encapsulation

  • Encapsulation allows you to group related data and functions together into a single unit known as object.
  • Encapsulation restricts access to these members (properties and methods) from outside of the object which helps to neutralize the problem of accidental modification of data.
class Document {
    private $title = '';
    private $body = '';
    public __construct( $title, $body ){
        $this->title = $title;
        $this->body = $body;
    }
    public function create_document(){
        $this->set_document_size();
    }
    protected function set_document_size(){}
}
$doc = new Document( 'My doc', 'Doc body' );
$doc->create_document();
/*
 * Once set, there will be no other way to change the data/properties
 * unless a method is provided by the Class!
 */

Abstraction

  • Abstraction hides the complexity (i.e., inner working principle) of an object from users. You only need to call the method and provide data if needed. That is all.
  • You will need to expose APIs to interact with the object though.
  • As long as the input and output doesn’t change, you can alter the inner working of an object whenever needed. This helps to update methods without worrying about users (whoever is calling the APIs).
// class Document {
    $property1 = '';
    public function print_document(){
        // some logic here...
    }
}
$document1 = new Document();
$document1->print_document();

/* 
 * here you don't need to know the logic "how the document is 
 * printed". just call print_document() and that's it!
 */

Inheritance

  • Inheritance removes redundant code from your application.
  • Inheritance helps to grow the application by introducing new classes that utilizes the existing class.
  • Idea here is to create a base (or in other words, very generic) class and inherit this base class whenever you need to make a very specific class.
  • For example; a base class can have very basic information while the inheriting class can have some more specific features added.
// base/generic class 'Document'
class Document {}

// more specific class 'PDF Document'
class PDF_Document extends Document{}

/*
 * You see, the PDF_Document class definitely contains more
 * features than Document class. Otherwise, there would
 * be no point in creating new class with same or less features.
 */

Polymorphism

  • Polymorphism will help to reference the correct object method depending upon the object reference.
  • It avoids writing of long if{}else{} or switch(){} block. Polymorphism feature automatically figures out which method is being referenced by user.
class Document {}
class PDF_Document extends Document{}
$doc1 = new Document();
$doc2 = new PDF_Document();
$document = $doc2->create_document();

/* here you don't have to worry about which method 'create_document' 
 * will be referenced. i.e., inside Document or PDF_Document. As long
 * as you are referencing correct object i.e., $doc2, compiler will 
 * know which one to invoke.
 * Note: Concept here is; you can override the parent method in 
 * child class!
 */

Benefits of using OOP

  • Modularity and reusability
  • Clear code and less complexity (because you don’t have much data freely flowing around)
  • Better code organization thus, easy to maintain
  • Updating the existing functionalities and addition of new features is easier

When to use oop?

  • For database driven and large applications.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.