Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
oop
communitycreator

What is OOP in PHP?

Saad Akbar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Object-Oriented Programming (OOP) is a specialized model based on objects and classes. It is used to simplify software programs into snippets of code blueprints (classes) that enable the creation of independent instances of objects. Objects contain data relevant to their classes and can access the class functions to perform different operations. Compared to OOP, procedural programming proposes creating functions or procedures that execute diverse operations on data.

Benefits of object-oriented over procedural programming

  • More effortless execution with faster speed.
  • Makes code maintainability easier.
  • Provides an easier to understand structure for the program.
  • Ensures the application of the "Don't Repeat Yourself" (DRY) principle, which focuses on filtering out the codes used repeatedly. This places those codes in a single area so that they can be reused and repetition is avoided.

Components

Two significant components of OOP are classes and objects.

A class named Bike has three objects called Honda, Kawasaki, and BMW.

A class is a template for objects, and an object is an instance of a class. An independent object inherits all the properties of the class. The difference lies in that each object will have different values for its properties.

Syntax

<?php
class XYZ {
  // code ...
}
?>

Example

<?php
class Bike {
  // Properties
  public $name;
  public $Engine_capacity;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }

  function set_Engine_capacity($Engine_capacity) {
    $this->Engine_capacity = $Engine_capacity;
  }
  function get_Engine_capacity() {
    return $this->Engine_capacity;
  }
}

$bike1 = new Bike();
$bike2 = new Bike();
$bike1->set_name('Honda');
$bike1->set_Engine_capacity('800cc');
$bike2->set_name('Kawasaki');
$bike2->set_Engine_capacity('1000cc');

echo $bike1->get_name();
echo $bike1->get_Engine_capacity();
echo "\n";
echo $bike2->get_name();
echo $bike2->get_Engine_capacity();
?>

Explanation

  • The following code shows the class Bike being declared. It has two properties, the name and the engine size.
  • Next, the setter and getter functions are declared for the properties of the name and engine size.
  • Consequently, two Bike objects are created and their values are set using the setter functions.
  • The getter functions are then used to print the information of each object.

RELATED TAGS

php
oop
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring