Assignments On Class 21 Object-Oriented Programming (OOP) - Part 3

Rashmi Mishra
0

 

Assignment Notes On Class 21

Object-Oriented Programming (OOP) - Part 3

Assignments of constructors and destructors in PHP. 


Assignment 1: Create a Student Class with a Constructor and Destructor

Task:
Create a 
Student class with the following properties:

  • name (student’s name)
  • id (student’s ID)
  • course (enrolled course)

The class should:

  1. Use a constructor to initialize these properties when a new object is created.
  2. Use a destructor to print a message indicating that the student’s record has been removed.

Assignment 2: Create a FileHandler Class for File Operations

Task:
Create a 
FileHandler class that:

  1. Uses a constructor to open a specified file in "write" mode.
  2. Uses a destructor to close the file when the object is destroyed.

Assignment 3: Create a BankAccount Class with Initialization and Cleanup

Task:
Design a 
BankAccount class with the following:

  1. A constructor that takes an accountNumber and balance as parameters and initializes them.
  2. A destructor that displays a message when the bank account object is no longer needed.

Assignment 4: Database Connection Class with Constructor and Destructor

Task:
Create a 
DatabaseConnection class that:

  1. Uses a constructor to connect to a database using mysqli_connect.
  2. Uses a destructor to close the connection when the object is destroyed.


SOLUTIONS 

Assignment Notes On Class 21

Object-Oriented Programming (OOP) - Part 3

Assignments of constructors and destructors in PHP. 


Assignment 1: Create a Student Class with a Constructor and Destructor

Task:
Create a 
Student class with the following properties:

  • name (student’s name)
  • id (student’s ID)
  • course (enrolled course)

The class should:

  1. Use a constructor to initialize these properties when a new object is created.
  2. Use a destructor to print a message indicating that the student’s record has been removed.

Solution

class Student {

    public $name;

    public $id;

    public $course;

 

    // Constructor

    public function __construct($name, $id, $course) {

        $this->name = $name;

        $this->id = $id;

        $this->course = $course;

        echo "Student record created: Name - $this->name, ID - $this->id, Course - $this->course<br>";

    }

 

    // Destructor

    public function __destruct() {

        echo "Student record deleted: Name - $this->name<br>";

    }

}

 

// Creating Student objects

$student1 = new Student("Alice", 101, "Computer Science");

$student2 = new Student("Bob", 102, "Mathematics");

 

// Destructor will be called automatically at the end of the script

Explanation

  • Constructor (__construct): When an object is created, the constructor initializes the nameid, and course properties and displays a message that the student record has been created.
  • Destructor (__destruct): When the object is no longer needed, the destructor displays a message saying the student record has been deleted.

Output:

Student record created: Name - Alice, ID - 101, Course - Computer Science

Student record created: Name - Bob, ID - 102, Course - Mathematics

Student record deleted: Name - Alice

Student record deleted: Name - Bob


Assignment 2: Create a FileHandler Class for File Operations

Task:
Create a 
FileHandler class that:

  1. Uses a constructor to open a specified file in "write" mode.
  2. Uses a destructor to close the file when the object is destroyed.

Solution

class FileHandler {

    private $file;

 

    // Constructor to open the file

    public function __construct($filename) {

        $this->file = fopen($filename, "w");

        if ($this->file) {

            echo "File '$filename' opened successfully.<br>";

        } else {

            echo "Error: Unable to open file '$filename'.<br>";

        }

    }

 

    // Destructor to close the file

    public function __destruct() {

        if ($this->file) {

            fclose($this->file);

            echo "File closed successfully.<br>";

        }

    }

}

 

// Creating a FileHandler object

$fileHandler = new FileHandler("example.txt");

 

// Destructor will be called automatically at the end of the script

Explanation

  • Constructor (__construct): This method opens a file named "example.txt" in write mode. If the file is successfully opened, it displays a confirmation message.
  • Destructor (__destruct): This method closes the file when the object is destroyed or at the end of the script, printing a message that the file has been closed.

Output:

File 'example.txt' opened successfully.

File closed successfully.


Assignment 3: Create a BankAccount Class with Initialization and Cleanup

Task:
Design a 
BankAccount class with the following:

  1. A constructor that takes an accountNumber and balance as parameters and initializes them.
  2. A destructor that displays a message when the bank account object is no longer needed.

Solution

class BankAccount {

    public $accountNumber;

    public $balance;

 

    // Constructor to initialize account number and balance

    public function __construct($accountNumber, $balance) {

        $this->accountNumber = $accountNumber;

        $this->balance = $balance;

        echo "Bank account created: Account Number - $this->accountNumber, Balance - $$this->balance<br>";

    }

 

    // Destructor to indicate account closure

    public function __destruct() {

        echo "Bank account for Account Number $this->accountNumber is now closed.<br>";

    }

}

 

// Creating BankAccount objects

$account1 = new BankAccount("123456", 1000);

$account2 = new BankAccount("789012", 5000);

 

// Destructor will be called automatically at the end of the script

Explanation

  • Constructor (__construct): Initializes the accountNumber and balance properties with the provided values. A message is displayed to indicate the account has been created.
  • Destructor (__destruct): When the object is no longer needed, the destructor prints a message indicating the account is closed.

Output:

Bank account created: Account Number - 123456, Balance - $1000

Bank account created: Account Number - 789012, Balance - $5000

Bank account for Account Number 123456 is now closed.

Bank account for Account Number 789012 is now closed.


Assignment 4: Database Connection Class with Constructor and Destructor

Task:
Create a 
DatabaseConnection class that:

  1. Uses a constructor to connect to a database using mysqli_connect.
  2. Uses a destructor to close the connection when the object is destroyed.

Solution

class DatabaseConnection {

    private $connection;

    // Constructor to initialize the database connection

    public function __construct($host, $username, $password, $database) {

        $this->connection = mysqli_connect($host, $username, $password, $database);

        if ($this->connection) {

            echo "Connected to database successfully.<br>";

        } else {

            echo "Error: Unable to connect to database - " . mysqli_connect_error() . "<br>";

        }

    }

 

    // Destructor to close the database connection

    public function __destruct() {

        if ($this->connection) {

            mysqli_close($this->connection);

            echo "Database connection closed.<br>";

        }

    }

}

 

// Creating a DatabaseConnection object

$dbConnection = new DatabaseConnection("localhost", "root", "", "my_database");

 

// Destructor will be called automatically at the end of the script

Explanation

  • Constructor (__construct): The constructor connects to a MySQL database using the provided host, username, password, and database name. A success or failure message is displayed based on the connection status.
  • Destructor (__destruct): When the object is no longer needed, the destructor closes the database connection, ensuring no open connections remain.

Output:

Connected to database successfully.

Database connection closed.


Summary

These assignments cover:

  1. Initialization of properties using constructors.
  2. Cleanup of resources using destructors.
  3. Practical application scenarios, such as managing database connections, file handling, and initializing object data.


Post a Comment

0Comments

Post a Comment (0)