Project: Blog app
Overview of the 5-lesson blog project: post management with OOP and forms.
Project: Blog app
In this 5-lesson project you'll build a blog app. Each lesson adds one piece, and by the final lesson you'll complete a working mini application.
You'll use what you learned in previous groups: classes, constructors, HTML forms and the foreach loop. Throughout the project you'll design a Post class that holds title, content and author and has a summary method. Then you'll add a Blog class to add and list multiple posts.
Project roadmap
The project has five lessons. In the second lesson you'll write the Post class and its constructor with properties for title, content and author. In the third you'll create new posts with an HTML form and $_POST, and make special characters safe with htmlspecialchars. In the fourth you'll walk the posts array with a foreach loop and display each post with a summary method. In the final lesson you'll combine all parts with a Blog class and complete the working blog app.
Exercise Define the Post class
Write the first piece of this project. Define a class named Post. Add a public string $title = ""; property (empty string default). Then create an instance with $p = new Post();, set the title with $p->title = "First post"; and print it with echo $p->title;.
Show example solution
Declare the class with class Post { ... }, add public string $title = "";. Create an instance with $p = new Post();, set the title with $p->title = "First post"; and print with echo $p->title;.
Example solution:
<?php
// Define the Post class and create an instance
class Post {
public string $title = "";
}
$p = new Post();
$p->title = "First post";
echo $p->title;
Test yourself
-
Why write a blog app with OOP?
- Post data and behavior stay in one unit; code is reusable.
- It can be written with loops only, OOP makes no difference.
- OOP does not work without a database.
- OOP is only for web pages.
Show answer
Post data and behavior stay in one unit; code is reusable. — Correct. A class bundles post data and behavior in one unit, so you can reuse the code in other projects.
-
Complete the post class name
class ____ { public string $title = ""; }Show answer
class Post { public string $title = ""; } -
Match the blog project concepts
- Post
- Holds post data
- Title
- Post title
- Content
- Post body
- Author
- Post writer