OOP Concepts: What's Your Biggest Challenge?

Max Zhuk - Jun 29 - - Dev Community

Hey, fellow developers!๐Ÿง‘๐Ÿผโ€๐Ÿ’ป

I'm currently working on a book about Object-Oriented Programming in PHP and TypeScript, and I'm curious about your experiences with OOP.

As developers, we all have those concepts that make us scratch our heads from time to time. So, I want to ask: Which OOP concept do you find most challenging to master?

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Drop your answer in the comments, and let's discuss why! Your insights might even help shape some explanations in my upcoming book.

To kick things off, here's a quick PHP 8 snippet showcasing polymorphism:

interface Logger
{
    public function log(string $message): void;
}

class ConsoleLogger implements Logger
{
    public function log(string $message): void
    {
        echo "Console: $message\n";
    }
}

class FileLogger implements Logger
{
    public function log(string $message): void
    {
        file_put_contents('log.txt', "File: $message\n", FILE_APPEND);
    }
}

function doSomething(Logger $logger)
{
    $logger->log("Operation completed");
}

doSomething(new ConsoleLogger());
doSomething(new FileLogger());
Enter fullscreen mode Exit fullscreen mode

This example demonstrates how different classes can implement the same interface, allowing for flexible and extensible code.

What's your take on polymorphism? Love it? Hate it? Share your thoughts!


Photo by rivage on Unsplash

. . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player