Lecture Notes: Class 19 - Introduction to Object-Oriented Programming (OOP) - Part 1

Rashmi Mishra
0

 

Lecture Notes: Class 19 - Introduction to Object-Oriented Programming (OOP) - Part 1


Objective:

  • Understand the basics of Object-Oriented Programming (OOP) in PHP.
  • Learn about classes and objects.

Outcome:

  • Students will be able to create classes and objects and understand basic OOP principles such as encapsulation.

1. Introduction to OOP

Definition: Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data and code that manipulates that data. It allows for modeling real-world entities, making software design more intuitive and manageable.

Key Characteristics of OOP:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

2. Key Concepts

2.1 Classes and Objects

  • Class: A blueprint for creating objects. It defines properties (attributes) and methods (functions) that the created objects will have.

Syntax:

php

Copy code

class ClassName {

    // Properties

    public $propertyName;

 

    // Methods

    public function methodName() {

        // code

    }

}

  • Object: An instance of a class. When a class is defined, no memory is allocated until an object is created from that class.

Creating an Object:

php

Copy code

$objectName = new ClassName();

2.2 Properties and Methods

  • Properties: Variables defined within a class that hold the data for the object.
  • Methods: Functions defined within a class that operate on the properties of the object.

Example:

php

Copy code

class Car {

    public $color; // Property

 

    public function setColor($newColor) { // Method

        $this->color = $newColor;

    }

 

    public function getColor() {

        return $this->color;

    }

}

3. Encapsulation

Definition: Encapsulation is the principle of restricting access to certain details of an object and exposing only the necessary parts. This is achieved using visibility keywords:

  • public: Accessible from anywhere.
  • private: Accessible only within the class itself.
  • protected: Accessible within the class and by inherited classes.

Example:

php

Copy code

class User {

    private $name; // Private property

 

    public function setName($name) {

        $this->name = $name; // Public method to set the private property

    }

 

    public function getName() {

        return $this->name; // Public method to get the private property

    }

}

4. Creating and Using Classes and Objects

4.1 Example: Implementing a Simple Class

Step 1: Define the Class

php

Copy code

class Dog {

    public $name;

    public $breed;

 

    public function bark() {

        return "Woof! Woof!";

    }

}

Step 2: Create an Object

php

Copy code

$dog1 = new Dog(); // Create an object of Dog class

$dog1->name = "Buddy"; // Set property

$dog1->breed = "Golden Retriever"; // Set property

 

echo $dog1->name; // Output: Buddy

echo $dog1->bark(); // Output: Woof! Woof!

5. Summary

  • OOP is a programming paradigm that helps structure code in a more manageable way.
  • Classes serve as blueprints for creating objects, which encapsulate data and behavior.
  • Encapsulation is essential for protecting an object's state and maintaining integrity.

6. Discussion Questions

  1. What is the main advantage of using OOP over procedural programming?
  2. How does encapsulation contribute to software security?
  3. Can you think of a real-world example where encapsulation can be applied?

7. Practice Exercise

  • Create a class named Book that includes properties for title, author, and price. Implement methods to set and get these properties.
  • Instantiate an object of the Book class, set its properties, and display its information.

Additional Resources


This concludes the first part of our introduction to Object-Oriented Programming in PHP. In the next class, we will explore more OOP principles like inheritance and polymorphism.


Post a Comment

0Comments

Post a Comment (0)