A quick guide to Object-Oriented Programming (OOP).

OOP [Object Oriented Programming]


object oriented programming in php, java, c++, dart


Why OOP [Object Oriented Programming ]?


Ans: OOP [Object Oriented Programming ] makes it easy to keep the code DRY principle [Do not Repeat Yourself]. The code written by the programmer should be reusable.


What is OOP?


Ans:

Basically, an object is treated like a real-life object or instance that has unique features i.e., a computer, desk, chair, or laptop.

Object-oriented is you write your code in terms of objects. So, OOP [Object Oriented Programming ] is a programming pattern or style using which you can create small code groups based on real-life objects like a cow, bus, etc.


  • The main purpose of OOP is to create a reusable code.
  • OOP brings modularity to your code

Modularity: Modularity means splitting your programs into smaller individual modules that can be separated and recombined.

  • When you are writing your code in OOP’ S style then you need to think about modularity/objects
  • You think and convert your code into real-life objects and use it.


Difference between Procedural language and Object-Oriented Language.

Ans:
Procedural language

Object-Oriented Language

Procedural language is thinking about how to write code from top to bottom by defining the variable and printing them.

Object-Oriented Language is thinking about how to define reusable components and use them (Bottom-up approach).

We define the variable.

We think of components.

We define functions.

We define components.

We define the logical flow of the Program.

We think about what variables and functions can be in that component.

The sequential flow of a program.

We focus on reusable code.


Start thinking about an object, forget about Procedural patterns for some time and try to think about an object.

Ans: 

Objects in oop


  1. Everything in the world can be defined as an object.
  2. Start thinking and organizing your code into smaller components and real-life objects.
  3. Stop thinking in procedural, variable, and function.
  4. Start thinking about only objects.

Note: In OOP variables are called properties, and functions are called methods. 


What is Class?

Ans:

Class is a Blueprint of an object. Class is the logical grouping of code with properties and methods specific to that object. The main purpose of the class is to organize your code into individual components.


Ans:

class in oop

Note: If a car is a class, then Ferrari is the object. And class can not be executed. To execute a class you need to define an object for it.

Creating class: Syntax

Class className{

// Properties

// Methods
}

Organize in classes

Ans:

  • Identify how you can organize code your code into objects. Example: users, car.
  • Start grouping into classes.
  • Each class has properties and methods for related individual objects.
  • One class can hold many properties and methods but logically represent that object.
  • Remember classes are the specification and not real.
organizing class in oop

Organize class in oop

What are properties and functions?

Ans:

Variables inside a class are called Properties and, methods that help perform some action in that class. methods are also called functions in Procedural programming.


properties and methods in oop


In the above example, public $name, public $color, public $size, public $size, and public $type are the properties of the class car. Same as function startCar() {}, and function increaseSpeed() {} are examples of methods.


What are objects?

Ans:

 Objects are used to access the properties and methods of a class. You need to create the object of a class then start accessing the properties and call the methods of that class. Objects are also called instances. You can create unlimited objects for a class. Each property is unique to that specific instance of a class.


object in oop

object in oop

object in oop

What is This keyword?

Ans:

  • This keyword allows us to access properties and methods of the same class.
  • This keyword indicates an instance of this class.
  • You don’t have to declare this keyword.
  • This keyword always refers to the properties and methods of that class.


How to access the class properties inside the class methods?

Ans:

We have 2 methods to access the properties:

  • Getter Method: Get method always has the return type
  • Setter method: Set method always has parameters in it.
<?php
 class Car {
    public $name ;

function setName($name){
    $this-> name = $name;
}
    function getName () : string{
        return $this-> name;
    }
 }
 $car1= new Car();
 $car1 -> setName("bmw");
 echo $car1 -> getName();


Examples of Setter and Getter methods

What are constructors?


Ans:

Every class has magic methods that get called automatically e.g.: $this. Constructors are magic methods that get loaded automatically when the instance of a class is created. You can choose to define the constructor method or ignore it as it is optional to define.


constructors in oop

purpose of the constructor


  • Initialize the property of the class.
  • Initialize the connection with the database.
  • Check if the file exists.
  • Open the file before using it with the methods.
  • Check the internet connection.
  • Check API is reachable before the connection.
  • Load an instance of a class.
  • Load mandatory properties to execute the methods in the class.


What are destructors?


Ans:

Destructors are magic methods that get loaded automatically just before the instance of a class is destroyed. The purpose of the destructor is to do clean-up activities. Example: closing database connection or saving file. You can choose to define the destructor method or ignore it as it is optional to define.


destructors in oop


purpose of destructors


  • Close the file connection.
  • Save the log file.
  • Write activity logs – end time.
  • Free up the resources.
  • Save the properties into a file.
  • Save the cache.
  • Serialize the objects.
  • perform the cleanup activities.
  • close the database activities.

Post a Comment

0 Comments