Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

Object-Oriented Principles in PHP: Ep 6, Interfaces as Feature Filters

The 'feature filters' described here are an implementation of the Interface Segregation Principle, or to put it simply, keeping interfaces focused on one specific task:

interface HasAuthor()
{
    public string $author { get; set; }
}

interface HasContent()
{
    public string $content { get; set; }    
}

interface CanBeLiked()
{
    public function like(): void;
    public function isLiked(): bool;
}

class Post implements HasAuthor, HasContent, CanBeLiked
{
    public string $author = '...';
    public string $content = '...';
    public function like(): void { ... }
    public function isLiked(): bool { ... }
}

This principle is more achievable in PHP now that we have Intersection Types. For example:

class Thread
{
    /** @var HasAuthor&HasContent&CanBeLiked[] */
    private array $replies = [];

    public function __construct(private HasAuthor&HasContent $subject)
    {
    }

    public function display()
    {
        echo $this->subject->content . '<br>';
        echo 'Author: ' . $this->subject->author . '<br><br>';
        foreach ($this->replies as $reply) {
            echo $reply->content . '<br>';
            echo 'Author: ' . $reply->author . '<br><br>';
            if ($reply->isLiked()) {
                echo '(Someone has liked this reply!)<br><br>';
            }
        }
    }
}

The Thread class doesn't know anything about a Post class; it just knows that its $subject property must implement both HasAuthor and HasContent interfaces and that $replies must be an array of elements implementing (via static analysis), HasAuthor, HasContent, and CanBeLiked. These could be instances of Post, Comment, Letter, Car, Dog... anything really. Thread doesn't care.

It's easy to go overboard with this approach, creating dozens of tiny interfaces with massive intersectional types. As with much in coding, try to strike a balance between readability and loose coupling!

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

Where are you all from?

Hello Jeffrey! I'm from Central Asia, Tajikistan! And I am excited 😊🙌🏻

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

Object-Oriented Principles in PHP: Ep 4, Dependencies, Coupling, and Interfaces

I’m thrilled by how attentively and with such joy Kermit listened to today’s lesson. <3 I recently watched the previous version of this series, and I have to admit—you did a damn good job refreshing this material. Keep it up!

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

PHP For Beginners: Ep 50, The Next Step in Your PHP Journey

Thank you for this wanderfull course!

Thank you very much for the course, once again. It was amazingly interesting!

artisanRunner's avatar

artisanRunner wrote a comment+100 XP

5mos ago

PHP For Beginners: Ep 50, The Next Step in Your PHP Journey

@JeffreyWay you're the best mentor! Thanks a lot for this course!

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

PHP For Beginners: Ep 21, Database Tables and Indexes

@junaidqadir There's an old saying in chinese. "He who by reviewing the old can gain knowledge of the new and is fit to be a teacher. "
alt image

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

Where are you all from?

It's fun to see that we span the globe. :)

artisanRunner's avatar

artisanRunner wrote a reply+100 XP

5mos ago

Where are you all from?

Hello Jeffrey! I'm from Central Asia, Tajikistan! And I am excited 😊🙌🏻

artisanRunner's avatar

artisanRunner liked a comment+100 XP

5mos ago

Where are you all from?

Chattanooga, Tennessee in the US here. :)