Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 8 of 8
Mini Project: Blog System - Part 2 (Functions & Array Processing)
Refactor the Blog System from Module 1 - replace hardcoded index access with named functions, build a PostCollection class using array functions (filter, sort, search), add date formatting with DateTime, and a clean slug-based URL router.
Blog Part 2 - Post Processing Engine
<?php
declare(strict_types=1);
// src/PostCollection.php
// A pure PHP class that wraps an array of posts with functional operations
class PostCollection {
private array $posts;
public function __construct(array $posts) {
$this->posts = $posts;
}
// Fluent interface - each method returns $this for chaining
public function published(): static {
return new static(array_values(
array_filter($this->posts, fn($p) => $p["published"] === true)
));
}
public function search(string $query): static {
return new static(array_values(
array_filter($this->posts, fn($p) =>
str_contains(mb_strtolower($p["title"]), mb_strtolower($query)) ||
str_contains(mb_strtolower($p["excerpt"]), mb_strtolower($query))
)
));
}
public function sortBy(string $field, string $direction = "asc"): static {
$sorted = $this->posts;
usort($sorted, function($a, $b) use ($field, $direction) {
$result = $a[$field] <=> $b[$field];
return $direction === "desc" ? -$result : $result;
});
return new static($sorted);
}
public function paginate(int $perPage, int $page): array {
$offset = ($page - 1) * $perPage;
return [
"items" => array_slice($this->posts, $offset, $perPage),
"total" => count($this->posts),
"per_page" => $perPage,
"current" => $page,
"last_page" => (int) ceil(count($this->posts) / $perPage),
];
}
public function findBySlug(string $slug): ?array {
foreach ($this->posts as $post) {
if ($post["slug"] === $slug) return $post;
}
return null;
}
public function toArray(): array { return $this->posts; }
public function count(): int { return count($this->posts); }
// Extract titles for sitemap, RSS feeds
public function getTitles(): array {
return array_column($this->posts, "title");
}
}
// Usage - clean, readable pipeline
$collection = new PostCollection($posts);
$results = $collection->published()->search("PHP")->sortBy("views", "desc");
$paginated = $results->paginate(perPage: 5, page: 1);
// Router (simple slug-based)
$requestSlug = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/");
$requestSlug = preg_replace("/[^a-z0-9-]/", "", $requestSlug); // sanitise
if ($requestSlug === "" || $requestSlug === "blog") {
$data = $collection->published()->sortBy("created_at", "desc")->paginate(5, 1);
include __DIR__ . "/../views/index.php";
} elseif ($post = $collection->findBySlug($requestSlug)) {
include __DIR__ . "/../views/post.php";
} else {
http_response_code(404);
include __DIR__ . "/../views/404.php";
}Quick Quiz
Module Quiz
Which PHP function applies a callback to every element of an array and returns the result?
Tip
Tip
Practice Mini Project Blog System Part 2 Functions Array Processing in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Mini Project: Blog System - Part 2 (Functions & Array Processing)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Blog System Part 2 Functions Array Processing from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Mini Project Blog System Part 2 Functions Array Processing is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?