1. Question: What are the key components of a class in PHP?
Solution: The key components of a class in PHP include properties (attributes that define
the state of an object), methods (functions that define the behavior of an object), and the
constructor (a special method used to initialize the object's properties when it is created).
Level 2: Understanding
2. Question: How does the concept of encapsulation improve the security of a PHP
application?
Solution: Encapsulation restricts access to certain properties and methods of a class by
using access modifiers (public, private, protected). This means that sensitive data and
implementation details are hidden from the outside, reducing the risk of unintended
interference and enhancing security.
Level 3: Applying
3. Question: Can you demonstrate how to create a class for a Car and instantiate two
objects of that class?
Solution:
php
Copy code
class Car {
public $make;
public $model;
public $year;
public function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
public function displayInfo() {
return "$this->year $this->make $this->model";
}
}
// Creating objects
$car1 = new Car("Toyota", "Camry", 2020);
$car2 = new Car("Honda", "Civic", 2021);
echo $car1->displayInfo(); // Outputs: 2020 Toyota Camry
echo $car2->displayInfo(); // Outputs: 2021 Honda Civic
Level 4: Analyzing
4. Question: Analyze the relationship between the Library and Book classes. How does
this relationship illustrate the concept of composition in OOP?
Solution: The Library class contains an array of Book objects, indicating a "has-a"
relationship where a library has many books. This illustrates composition, where the
lifecycle of the Book objects is managed by the Library class; if the Library object is
destroyed, the Book objects are also destroyed since they are part of the library's
collection.
Level 5: Evaluating
5. Question: Evaluate the advantages and disadvantages of using inheritance in PHP
applications.
Solution: Advantages:
o Promotes code reusability by allowing subclasses to inherit properties and
methods from a parent class.
o Facilitates a clear hierarchy and relationship between classes, making code easier
to manage.
o Allows for polymorphism, where a parent class reference can refer to child class
objects.
Disadvantages:
o Can lead to a complex class hierarchy, making the system harder to understand
and maintain.
o Changes in the parent class can unintentionally affect all subclasses, introducing
potential bugs.
o Overuse of inheritance can lead to a tightly coupled system, reducing flexibility.
Level 6: Creating
6. Question: Design a simple Inventory class that manages a collection of products,
including methods to add and remove products. How would you implement this
functionality?
Solution:
php
Copy code
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
class Inventory {
private $products = [];
public function addProduct(Product $product) {
$this->products[] = $product;
}
public function removeProduct($productName) {
foreach ($this->products as $key => $product) {
if ($product->name === $productName) {
unset($this->products[$key]);
return "Removed: " . $productName;
}
}
return "Product not found: " . $productName;
}
public function displayProducts() {
foreach ($this->products as $product) {
echo $product->name . " - $" . $product->price . "<br>";
}
}
}
// Example usage
$inventory = new Inventory();
$product1 = new Product("Laptop", 999.99);
$product2 = new Product("Smartphone", 499.99);
$inventory->addProduct($product1);
$inventory->addProduct($product2);
$inventory->displayProducts();
These questions are designed to engage students in critical thinking and practical application of
OOP concepts in PHP. You can adapt the complexity of the questions and solutions based on
your students' knowledge and experience levels.