pg7
pg7
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;
// Creating objects
$car1 = new Car("Toyota", "Camry", 2020);
$car2 = new Car("Honda", "Civic", 2021);
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
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
php
Copy code
class Product {
public $name;
public $price;
class Inventory {
private $products = [];
// 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.