Menu
Coddy logo textTech

Recap - Custom Collection

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 50 of 91.

challenge icon

Challenge

Easy

Let's build a Collection class that brings together all the magic methods you've learned in this chapter. Your collection will act as a flexible data container that feels natural to use — storing items by key, checking if they exist, finding items dynamically, and creating independent copies.

You'll organize your code across two files:

  • Collection.php — Create a Collection class that stores items in a private array. Your collection should support these magic methods working together:
    • __construct() — Accepts an optional array of initial items (default to empty array)
    • __get($key) — Returns the item at the given key, or null if it doesn't exist
    • __set($key, $value) — Stores the value at the given key
    • __isset($key) — Returns whether the key exists in the collection
    • __unset($key) — Removes the item at the given key
    • __call($method, $args) — Handles dynamic finder methods. When a method starting with findBy is called (like findByRole or findByStatus), extract the field name (lowercase the part after "findBy"), search through all items, and return the key of the first item where that field matches the first argument. Return null if no match is found. For any other method, return "Unknown method: [method]"
    • __toString() — Returns "Collection([count] items)" where count is the number of items
    • __clone() — When cloned, the items array should be a fresh copy (not a reference to the original)
  • main.php — Include the Collection file. You'll receive three inputs: a key name, a role value, and a search role. Build a collection of users and demonstrate all the magic methods:
    • Create a new Collection
    • Add a user with the input key, storing an array with "name" set to the key and "role" set to the input role value
    • Add another user with key "bob", storing ["name" => "Bob", "role" => "editor"]
    • Print whether the input key exists using isset() — output "exists" or "missing"
    • Print the result of calling the dynamic findByRole() method with the search role as the argument (this should return the key of the matching user, or nothing if null)
    • Print the collection using echo (triggers __toString)
    • Clone the collection, then unset the input key from the clone
    • Print whether the input key exists in the original collection — output "exists" or "missing" (should still exist since clone is independent)

Each output should be on its own line. The dynamic finder assumes each item in your collection is an associative array with fields like "name" and "role".

Try it yourself

<?php

require_once 'Collection.php';

// Read inputs
$keyName = trim(fgets(STDIN));
$roleValue = trim(fgets(STDIN));
$searchRole = trim(fgets(STDIN));

// TODO: Create a new Collection

// TODO: Add a user with the input key, storing array with "name" => key and "role" => roleValue

// TODO: Add another user with key "bob", storing ["name" => "Bob", "role" => "editor"]

// TODO: Print whether the input key exists using isset() - output "exists" or "missing"

// TODO: Print the result of calling findByRole() with searchRole as argument

// TODO: Print the collection using echo (triggers __toString)

// TODO: Clone the collection, then unset the input key from the clone

// TODO: Print whether input key exists in the ORIGINAL collection - "exists" or "missing"

?>

All lessons in Object Oriented Programming