PHP
Project final - Blog
Combine all parts and finalize a working blog app.
Combine all parts
In the final lesson you'll combine all parts. The Post class (properties, constructor, summary) is ready. Now you'll manage multiple posts in one package with a Blog class.
The Blog class's public array $posts = []; property holds posts in an array. The addPost(Post $post): void method adds a new post, and the listPosts(): string method joins all post summaries and returns them as a single string. So you can collect posts from a form and show them in one output.
Managing posts with the Blog class
In the Blog class the addPost method appends a new post to the array with $this->posts[] = $post;. The listPosts method walks all posts with foreach, calls summary() on each, and joins the results in an $output variable.
Usage: create an empty blog with $blog = new Blog();, add posts with $blog->addPost(new Post(...));, and print the whole list with echo $blog->listPosts();. This pattern is the core of real blog systems.
Exercise Add the Blog class and run the blog
After the Post class, define a Blog class. Add a public array $posts = []; property. Write a public function addPost(Post $post): void method; inside it assign $this->posts[] = $post;. Add a public function listPosts(): string method; inside it initialize $output = "";, walk with foreach ($this->posts as $post), append with $output .= $post->summary() . "\n"; and return with return $output;. In the last lines create a blog with $blog = new Blog();, add two posts and print with echo $blog->listPosts();.
Show example solution
void { $this->posts[] = $post; } public function listPosts(): string { $output = ""; foreach ($this->posts as $post) { $output .= $post->summary() . "\n"; } return $output; } }. Run with $blog = new Blog(); $blog->addPost(new Post("First post", "Hello world", "Ayşe")); $blog->addPost(new Post("Second post", "PHP is nice", "Alex")); echo $blog->listPosts();.
Example solution:
<?php
// Combine all parts: Post + Blog classes
class Post {
public string $title = "";
public string $content = "";
public string $author = "";
public function __construct(string $title, string $content, string $author) {
$this->title = $title;
$this->content = $content;
$this->author = $author;
}
public function summary(): string {
return $this->title . " - " . $this->author;
}
}
class Blog {
public array $posts = [];
public function addPost(Post $post): void {
$this->posts[] = $post;
}
public function listPosts(): string {
$output = "";
foreach ($this->posts as $post) {
$output .= $post->summary() . "\n";
}
return $output;
}
}
$blog = new Blog();
$blog->addPost(new Post("First post", "Hello world", "Ayşe"));
$blog->addPost(new Post("Second post", "PHP is nice", "Alex"));
echo $blog->listPosts();
Project complete
Congratulations! You finished the blog app project. Across five lessons you designed a Post class with properties, a constructor and a summary method. Then you learned to create new posts with an HTML form and $_POST, and to show a post list with foreach. Finally you combined all parts with a Blog class.
You can now reuse these classes on a real web page: when the form is submitted, create a Post instance, add it to the Blog, and list everything on the home page. Bundling data and behavior with OOP makes the code easier to maintain and extend.
Test yourself
-
What's needed to run the blog app?
- Define Post and Blog classes, instantiate and call methods
- Just write an echo statement
- Set up a database connection
- Load an external CSS file
Show answer
Define Post and Blog classes, instantiate and call methods — Correct. Defining the classes, instantiating and calling methods is enough; the blog runs in the console.
-
Complete the body of the addPost method
public function addPost(Post $post): void {
$this->posts[] = ____;
}
Show answer
public function addPost(Post $post): void {
$this->posts[] = $post;
}
-
Match the blog app pieces
- Post
- Post class
- Blog
- Manages posts
- addPost()
- Adds a post
- listPosts()
- Returns the list
Next lesson: Reading with file_get_contents()