Project final - Library
Combine all parts and finalize the library as a working module.
Finalizing the library
In the final lesson you'll complete the library. Add an all() method to the class: it returns the internal $this->items array. This gives safe outside access to the items.
Then combine every part: write the class to a file, include it with require_once, add items with chaining, and print all items in one line with implode. This is a working library module.
Accessing items with all()
Making a property public opens it to direct outside changes. A safer way is to write an all() method that returns the items: public function all(): array { return $this->items; }.
You can combine this method with implode to print all items in one line: echo implode(", ", $c->all());. The collection is now both safe and convenient.
Exercise Add the all() method and use the library
Inside the class (that is, the $classCode string), add a public function all(): array { return $this->items; } method. Write the file with file_put_contents, include it with require_once. Then start with $c = new Collection(["apple"]);, chain $c->add("pear")->add("cherry"); and print all items with echo implode(", ", $c->all());.
Show example solution
array { return $this->items; } inside the $classCode string. Include with require_once. Start with new Collection(["apple"]), chain $c->add("pear")->add("cherry");, print with echo implode(", ", $c->all());.
Example solution:
<?php
// Finalize the library: add the all() method and use all features
$classCode = '<?php
class Collection {
public array $items = [];
public function __construct(array $initial = []) {
$this->items = $initial;
}
public function add(string $item) {
$this->items[] = $item;
return $this;
}
public function all(): array {
return $this->items;
}
}';
$classFile = __DIR__ . "/Collection.php";
file_put_contents($classFile, $classCode);
require_once $classFile;
$c = new Collection(["apple"]);
$c->add("pear")->add("cherry");
echo implode(", ", $c->all());
Project complete
Congratulations! You finished the utility library project. Across five lessons you designed a Collection class: a property, a constructor, add and find methods, chaining and file inclusion.
You can now use this class in any project: put it in a Collection.php file, include it with require_once __DIR__ . "/Collection.php"; and use it with new Collection(). This is the basic way to organize reusable code in real PHP projects.
Test yourself
-
How do you share a library module across projects?
- Put the class in a separate file and include with require_once
- Copy and paste the class into every file
- Keep the class in one long file
- Write the class once and never use it
Show answer
Put the class in a separate file and include with require_once — Correct. Putting the class in its own file and including it with require_once makes reuse and maintenance easy.
-
Match the library concepts
- Collection
- Class that holds items
- all()
- Returns the items
- require_once
- Includes class from file
- implode
- Joins items together
-
Make all() return items
public function all(): array { return ____ }Show answer
public function all(): array { return $this->items; }