CodeBlanks Logo Icon CodeBlanks

Creating a post class

PHP course · Intermediate · Lesson 66/115

Design a Post class that holds title, content and author.

Post class properties

A post carries three core pieces of data: title, content and author. In the Post class you declare three properties for this data: public string $title = "";, public string $content = ""; and public string $author = "";. Each property starts with an empty string default.

Declaring the properties explicitly in the class is good practice: the type string and default "" are clear. Every instance starts in a consistent state and debugging becomes easier.

Setup with __construct

Instead of assigning three properties by hand one by one, you can pass all three values together when creating an instance with the __construct method. You write public function __construct(string $title, string $content, string $author) and inside it add assignments like $this->title = $title;.

So new Post("First post", "Hello world", "Ayşe") gives you a ready post. The $this pseudo-variable points to the instance being created and lets you copy the parameter to the property.

Exercise Add properties and a constructor

Add public string $content = ""; and public string $author = ""; properties to the Post class. Then write a public function __construct(string $title, string $content, string $author) constructor; inside it assign $this->title = $title;, $this->content = $content;, $this->author = $author;. Update the instance to $p = new Post("First post", "Hello world", "Ayşe"); and change the last line to echo $p->title . " - " . $p->author;.

Show example solution
Add public string $content = ""; and public string $author = ""; to the class. Write public function __construct(string $title, string $content, string $author) { $this->title = $title; $this->content = $content; $this->author = $author; }. Create an instance with $p = new Post("First post", "Hello world", "Ayşe"); and print with echo $p->title . " - " . $p->author;.
Example solution:
<?php
// Add properties and a constructor to the Post class
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;
    }
}

$p = new Post("First post", "Hello world", "Ayşe");
echo $p->title . " - " . $p->author;

Test yourself

  1. Why is the constructor useful?

    • To hide the class name
    • To pass data once when creating an instance and assign it to properties
    • To delete array elements
    • To change a variable's type
    Show answer

    To pass data once when creating an instance and assign it to properties — Correct. The constructor takes data as parameters when an instance is created with new and assigns it to properties, so the instance is ready in one line.

  2. Add the parameter list to the constructor

    public function __construct(string ____) {
        $this->title = $title;
        $this->content = $content;
        $this->author = $author;
    }
    Show answer
    public function __construct(string $title, string $content, string $author) {
        $this->title = $title;
        $this->content = $content;
        $this->author = $author;
    }
  3. Match the constructor concepts

    __construct
    Constructor name
    $this->title
    Property assignment
    string $title
    Title parameter
    new Post(...)
    Instantiation

All lessons in Intermediate