Experiment 9 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Experiment 9

Title : Experiment Based on advanced OOP concepts in PHP

Aim : To make student understand the advanced OOP concepts using


PHP and usage of the same

Advanced OOP concepts


Following are the advanced Object Oriented Concepts in PHP:-
 Inheritance - When a class derives from another class it is called as
inheritance. The child class will inherit all the properties and methods
from the parent class. In addition, it can have its own properties and
methods. An inherited class is defined by using the extends keyword.

 Access Modifiers - Properties and methods can have access


modifiers which control where they can be accessed.
There are three access modifiers:
public - the property or method can be accessed from everywhere.
This is default
protected - the property or method can be accessed within the class
and by classes derived from that class
private - the property or method can ONLY be accessed within the
class

 Constants - Class constants can be useful if you need to define


some constant data within a class. A class constant is declared
inside a class with the const keyword. A constant cannot be
changed once it is declared. Class constants are case-sensitive.
However, it is recommended to name the constants in all uppercase
letters. We can access a constant from outside the class by using
the class name followed by the scope resolution operator (::)
followed by the constant name.

 Abstract Classes - Abstract classes and methods are when the


parent class has a named method, but need its child class(es) to fill
out the tasks. An abstract class is a class that contains at least one
abstract method. An abstract method is a method that is declared,
but not implemented in the code. An abstract class or method is
defined with the abstract keyword.
 Interfaces - Interfaces allow you to specify what methods a class
should implement. Interfaces are declared with the interface
keyword. A class that implements an interface must implement all
of the interface's methods.

The difference between interfaces and abstract classes are:

o Interfaces cannot have properties, while abstract classes can


o All interface methods must be public, while abstract class
methods is public or protected
o All methods in an interface are abstract, so they cannot be
implemented in code and the abstract keyword is not
necessary
o Classes can implement an interface while inheriting from
another class at the same time

 Static methods - Static methods can be called directly - without


creating an instance of the class first.
o Static methods are declared with the static keyword
o To access a static method use the class name, double colon
(::), and the method name
o A static method can be accessed from a method in the same
class using the self keyword and double colon (::)
o Static methods can also be called from methods in other
classes. To do this, the static method should be public
o To call a static method from a child class, use the parent
keyword inside the child class. Here, the static method can be
public or protected

 Static properties - Static properties can be called directly -


without creating an instance of a class.
o Static properties are declared with the static keyword
o A static property can be accessed from a method in the same
class using the self keyword and double colon
o To call a static property from a child class, use the parent
keyword inside the child class

<?php

// Abstract class
abstract class Media {
protected $title;
protected $author;
const MEDIA_TYPE = 'General';

public function __construct($title, $author) {


$this->title = $title;
$this->author = $author;
}

abstract public function getDetails();

public static function getMediaType() {


return static::MEDIA_TYPE;
}
}

// Interface
interface Borrowable {
public function borrow($borrower);
}

// Inheritance
class Book extends Media implements Borrowable {
private $isbn;
public static $totalBooks = 0;

const MEDIA_TYPE = 'Book';

public function __construct($title, $author, $isbn) {


parent::__construct($title, $author);
$this->isbn = $isbn;
self::$totalBooks++;
}

public function getDetails() {


return "Title: {$this->title}, Author: {$this->author}, ISBN: {$this->isbn}, Type: " .
self::getMediaType();
}

public function borrow($borrower) {


return "{$borrower} has borrowed the book: {$this->title}";
}
}

class Magazine extends Media implements Borrowable {


private $issueNumber;
public static $totalMagazines = 0;

const MEDIA_TYPE = 'Magazine';

public function __construct($title, $author, $issueNumber) {


parent::__construct($title, $author);
$this->issueNumber = $issueNumber;
self::$totalMagazines++;
}

public function getDetails() {


return "Title: {$this->title}, Author: {$this->author}, Issue: {$this->issueNumber},
Type: " . self::getMediaType();
}

public function borrow($borrower) {


return "{$borrower} has borrowed the magazine: {$this->title}";
}
}

// Usage
$libraryItems = [
new Book("1984", "George Orwell", "123456789"),
new Magazine("National Geographic", "Various", "2023-09")
];

foreach ($libraryItems as $item) {


echo $item->getDetails() . PHP_EOL;
echo $item->borrow("John Doe") . PHP_EOL;
}

// Accessing static properties


echo "Total Books: " . Book::$totalBooks . PHP_EOL;
echo "Total Magazines: " . Magazine::$totalMagazines . PHP_EOL;

?>

Conclusion:-

Thus, we have studied, understood and practically checked Advanced OOP concepts in
php.

You might also like